Making HTTP POST requests with the browsable API

Now, we want to use the browsable API to compose and send an HTTP POST request to our RESTful Web Service to create a new toy. Go to the following URL in your web browser, http://localhost:8000/toys/. At the bottom of the rendered web page, the browsable API displays the following controls to allow us to compose and send a POST request to /toys/:

  • Media type: This dropdown allows us to select the desired parser. The list will be generated based on the configured supported parsers in the Django REST framework for our web service.
  • Content: This text area allows us to enter the text for the body that will be sent with the POST request. The content must be compatible with the selected value for the media type dropdown.
  • POST: This button will use the selected media type and the entered content to compose and send an HTTP POST request with the appropriate header key/value pairs and content.

The following screenshot shows the previously explained controls at the bottom of the rendered web page:

We enabled all the default parsers provided by the Django REST framework for our RESTful Web Service, and therefore, the Media type drop-down will provide us with the following options:

  • application/json
  • application/x-www-form-urlencoded
  • multipart/form-data

Select application/json in the Media type dropdown and enter the following JSON content in the Content text area:

{ 
    "name": "Surfer girl", 
    "description": "Surfer girl doll", 
    "toy_category":"Dolls", 
    "was_included_in_home": "false", 
    "release_date": "2017-10-29T12:11:25.090335Z" 
} 

Click or tap POST. The browsable API will compose and send an HTTP POST request to /toys/ with the previously specified data as a JSON body, 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 dropdown and text area with the POST button to allow us to continue composing and sending POST requests to /toys/:

In this case, we entered the JSON key/value pairs as we did when we composed and sent HTTP POST requests with command-line and GUI tools. However, we will learn to configure the browsable API to provide us with a form with fields to make it even easier to perform operations on our RESTful Web Service.
..................Content has been hidden....................

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