Using a web browser to work with our web service

Let's start browsing our RESTful Web Service. Open a web browser and enter http://localhost:8000/toys/. The browser will compose and send a GET request to http://localhost:8000/toys/ with text/html as the desired content type and the returned HTML web page will be rendered.

Under the hood, the web service will compose and send an HTTP GET request to http://localhost:8000/toys/ with application/json as the content type and the headers, and the JSON returned by this request will be rendered as part of the content of the web page. The following screenshot shows the rendered web page with the resource collection description, Toy List:

When we work with the browsable API, Django uses the information about the allowed methods for a resource or resource collection to render the appropriate buttons to allow us to execute the related requests. In the previous screenshot, you will notice that there are two buttons on the right-hand side of the resource description (Toy List): OPTIONS and GET. We will use the different buttons to make additional requests to the RESTful Web Service.

If you decide to browse the web service in a web browser that is being executed on another computer or device connected to the LAN, you will have to use the assigned IP address to the computer that is running Django's development server instead of localhost. For example, if Django's development server is running on a computer whose assigned IPv4 IP address is 192.168.2.125, instead of http://localhost:8000/toys/, you should use http://192.168.2.125:8000/toys/. You can also use the hostname instead of the IPv4 address or an IPv6 address.

One of the nicest features of the browsable API is that it makes it extremely easy to test a RESTful Web Service from a mobile device.

As a disclaimer, I must say that once you learn how to take advantage of the browsable API, you will never want to work with a framework that doesn't provide a feature like this one.

..................Content has been hidden....................

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