Understanding accepted and returned content types

So far, our RESTful Web Service has been working with JSON for the response body. The code we wrote in the toys/views.py file in Chapter 13Creating API Views, declares a JSONResponse class and two function-based views. These functions return a JSONResponse when it is necessary to return JSON data and a django.Http.Response.HttpResponse instance when the response is just an HTTP status code. No matter the accepted content type specified in the HTTP request header, the view functions always provide the same content in the response body: JSON.

Run the following command to retrieve all the toys with the Accept request header key set to text/html. Remember that the virtual environment we have created in Chapter 13Creating API Views, must be activated in order to run the next http command:

    http :8000/toys/ Accept:text/html 

The following is the equivalent curl command:

    curl -H "Accept: text/html" -iX GET localhost:8000/toys/

The previous commands will compose and send the following HTTP request: GET http://localhost:8000/toys/. These commands specify the text/html value for the Accept key in the request header. This way, the HTTP request indicates that it accepts a response of text/html.

The header response for the request will include the following line:

    Content-Type: application/json 

Now, run the following command to retrieve all the toys with different values with the Accept request header key set to text/html.

Run the following command to retrieve all the toys with the Accept request header key set to application/json:

   http :8000/toys/ Accept:application/json

The following is the equivalent curl command:

  curl -H "Accept: application/json" -iX GET localhost:8000/toys/

The previous commands will compose and send the following HTTP request: GET http://localhost:8000/toys/. These commands specify the application/json value for the Accept key in the request header. This way, the HTTP request indicates that it accepts a response of application/json.

The header response for the request will include the following line:

    Content-Type: application/json

The first group of commands defined the text/html value for the Accept request header key. The second group of commands defined the application/json value for the Accept request header key. However, both produced the same results and the responses were always in the JSON format. The view functions don't take into account the value specified for the Accept request header key in the HTTP requests. No matter the value indicated for the Accept request header key, the response is always in the JSON format.

We want to provide support for other formats. However, we don't want to write a huge amount of code to do so. Thus, we will take advantage of additional features included in the Django REST framework that will make it easy for us to support additional formats for 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
52.15.245.1