Making HTTP DELETE requests

Now, we will compose and send an HTTP request to delete an existing toy, specifically, the last toy we added. As in our last HTTP request, we have to check the value assigned to pk in the previous response and replace 4 in the command with the returned value:

    http DELETE :8000/toys/4

The following is the equivalent curl command:

    curl -iX DELETE localhost:8000/toys/4

The previous commands will compose and send the following HTTP request: DELETE http://localhost:8000/toys/4. The request has a number after /toys/, and therefore, it will match the '^toys/(?P<pk>[0-9]+)$' regular expression 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 DELETE, the request.method property is equal to 'DELETE', and therefore, the function will execute the code that parses the JSON data received in the request. Then, the function creates a Toy instance from this data and deletes the existing toy in the database. If the toy was successfully deleted in the database, the function returns an HTTP 204 No Content status code. The following lines show an example response to the HTTP request after successfully deleting an existing toy:

    HTTP/1.0 204 No Content
    Content-Length: 0
    Content-Type: text/html; charset=utf-8
    Date: Tue, 10 Oct 2017 17:45:40 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
18.188.66.13