Browsing an API with relationships

Now we can launch Django's development server to compose and send HTTP requests to our still unsecured, yet much more complex, web API (we will definitely add security later). Execute any of the following two commands based on your needs to access the API in other devices or computers connected to your LAN. Remember that we analyzed the difference between them in the previous chapter:

    python manage.py runserver
    python manage.py runserver 0.0.0.0:8000

After we run any of the previous commands, the development server will start listening at port 8000.

Open a web browser and enter http://localhost:8000/ or the appropriate URL if you are using another computer or device to access the browsable API. The browsable API will compose and send a GET request to / and will display the results of its execution, that is, the headers and the JSON response from the execution of the get method defined in the ApiRoot class within the views.py file. The following screenshot shows the rendered web page after entering the URL in a web browser with the resource description of api-root:

The api-root provides us with hyperlinks to see the list of ESRB ratings, games, players, and scores. This way, it becomes extremely easy to access the lists and perform operations on the different resources through the browsable API. In addition, when we visit the other URLs, the breadcrumb will allow us to go back to the api-root.

In this new version of the API, we worked with the generic views that provide many functions under the hood, and therefore, the browsable API will provide us additional features compared with the previous version. Click or tap on the URL at the right-hand side of "esrb-ratings". If you are browsing in localhost, the URL will be http://localhost:8000/esrb-ratings/. The browsable API will render the web page for the ESRB rating List.

At the bottom of the rendered web page, the browsable API provides us some controls to generate a POST request to /esrb-ratings/. In this case, by default, the browsable API displays the HTML form tab with an automatically generated form that we can use to generate a POST request without having to deal with the raw data as we did in our previous version. The HTML forms make it easy to generate requests to test our API. The following screenshot shows the HTML form to create a new ESRB rating:

We just need to enter the desired name, AO (Adults Only), in the Name textbox and click or tap POST to create a new ESRB rating. The browsable API will compose and send a POST request to /esrb-ratings/ with the previously specified data and we will see the results of the call in the web browser. The following screenshot shows a web browser displaying the HTTP status code 201 Created in the response and the previously explained HTML form with the POST button to allow us to continue composing and sending POST requests to /esrb-ratings/:

Now click on the URL displayed as a value for the "url" key in the JSON data displayed for the ESRB rating, such as http://localhost:8000/esrb-ratings/1/. Make sure you replace 1 with the ID of an existing ESRB rating in the previously rendered esrbrating-list. The browsable API will compose and send a GET request to /esrb-ratings/1/ and will display the results of its execution, that is, the headers and the JSON data for the ESRB rating. The web page will display a DELETE button because we are working with the ESRB rating Detail view.

We can use the breadcrumb to go back to the API root and start creating games related to an ESRB rating, players, and finally, scores related to a game and a player. We can do all this with easy-to-use HTML forms and the browsable API feature. This feature is extremely useful for testing CRUD operations with a RESTful API.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.145.114