Making HTTP GET requests that target a single instance

Now, we will make HTTP GET requests that target a single Toy instance. We will select one of the toys from the previous list and we will compose an HTTP request to retrieve only the chosen toy. For example, in the previous list, the first toy has a pk value equal to 3 because the results are ordered by the toy's name in ascending order. Run the following command to retrieve this toy. Use the pk value you have retrieved in the previous command for the first toy, as the pk number might be different if you execute the sample code or the commands more than once or you make changes to the toys_toy table. In this case, you don't have to enter an ending slash (/) because /toys/3/ won't match any of the patterns specified in urlpatterns in the toys/urls.py file:

    http :8000/toys/3  

The following is the equivalent curl command:

    curl -iX GET localhost:8000/toys/3

The previous commands will compose and send the following HTTP request: GET http://localhost:8000/toys/3/. The request has a number after /toys/, and therefore, it will match '^toys/(?P<pk>[0-9]+)$' and run the views.toy_detail function, that is, the toy_detail function declared within the toys/views.py file. The function receives request and pk as parameters because the URL pattern passes the number specified after /toys/ in the pk parameter.

As the HTTP verb for the request is GET, the request.method property is equal to 'GET', and therefore, the toy_detail function will execute the code that retrieves the Toy object whose primary key matches the pk value received as an argument and, if found, generates a JSON response with this Toy object serialized. The following lines show an example response for the HTTP request, with the Toy object that matches the pk value in the JSON response:

    HTTP/1.0 200 OK
    Content-Length: 182
    Content-Type: application/json
    Date: Tue, 10 Oct 2017 04:24:35 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    X-Frame-Options: SAMEORIGIN
    
    {
        "description": "6 figures from Clash Royale", 
        "name": "Clash Royale play set", 
        "pk": 3, 
        "release_date": "2017-10-09T12:10:00.776594Z", 
        "toy_category": "Playset", 
        "was_included_in_home": false
    }

Now, we will compose and send an HTTP request to retrieve a toy that doesn't exist. For example, in the previous list, there is no toy with a pk value equal to 17500. Run the following command to try to retrieve this toy. Make sure you use a pk value that doesn't exist. We must make sure that the utilities display the headers as part of the response because the response won't have a body:

    http :8000/toys/17500  

The following is the equivalent curl command:

    curl -iX GET localhost:8000/toys/17500

The previous commands will compose and send the following HTTP request: GET http://localhost:8000/toys/17500. The request is the same as the previous one we analyzed, with a different number for the pk parameter. The server will run the views.toy_detail function, that is, the toy_detail function declared within the toys/views.py file. The function will execute the code that retrieves the Toy object whose primary key matches the pk value received as an argument and a Toy.DoesNotExist exception will be thrown and captured because there is no toy with the specified pk value. Thus, the code will return an HTTP 404 Not Found status code. The following lines show an example header response for the HTTP request:

    HTTP/1.0 404 Not Found
    Content-Length: 0
    Content-Type: text/html; charset=utf-8
    Date: Tue, 10 Oct 2017 15:54:59 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    X-Frame-Options: SAMEORIGIN
..................Content has been hidden....................

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