Making HTTP PUT requests

Now, we will compose and send an HTTP request to update an existing toy, specifically, the previously added toy. We have to check the value assigned to pk in the previous response and replace 4 in the command with the returned value. For example, if the value for pk was 4, you should use :8000/toys/4 instead of :8000/toys/4:

http PUT :8000/toys/4 name="PvZ 3 puzzle" description="Plants vs Zombies 3 puzzle" toy_category="Puzzles & Games" was_included_in_home=false release_date="2017-10-08T01:01:00.776594Z"

The following is the equivalent curl command. As with the previous curl example, it is very important to use the -H "Content-Type: application/json" option to indicate curl to send the data specified after the -d option as application/json instead of the default application/x-www-form-urlencoded:

curl -iX PUT -H "Content-Type: application/json" -d '{"name":"PvZ 3 puzzle", "description":"Plants vs Zombies 3 puzzle", "toy_category":"Puzzles & Games", "was_included_in_home": "false", "release_date": "2017-10-08T01:01:00.776594Z"}' localhost:8000/toys/4

The previous commands will compose and send the following HTTP request: PUT http://localhost:8000/toys/4 with the following JSON key-value pairs:

{ 
    "name": "PvZ 3 puzzle",  
    "description":"Plants vs Zombies 3 puzzle", 
    "toy_category":"Puzzles & Games", 
    "was_included_in_home": "false", 
    "release_date": "2017-10-08T01:01:00.776594Z" 
}

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 PUT, the request.method property is equal to 'PUT', and therefore, the function will execute the code that parses the JSON data received in the request. Then, the function will create a Toy instance from this data and update the existing toy in the database. If the toy was successfully updated in the database, the function returns an HTTP 200 OK status code and the recently updated Toy serialized to JSON in the response body. The following lines show an example response for the HTTP request, with the updated Toy object in the JSON response:

    HTTP/1.0 200 OK
    Content-Length: 180
    Content-Type: application/json
    Date: Tue, 10 Oct 2017 17:06:43 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    X-Frame-Options: SAMEORIGIN
    
    {
        "description": "Plants vs Zombies 3 puzzle", 
        "name": "PvZ 3 puzzle", 
        "pk": 4, 
        "release_date": "2017-10-08T01:01:00.776594Z", 
        "toy_category": "Puzzles & Games", 
        "was_included_in_home": false
    }

In order to successfully process a PUT HTTP request that updates an existing toy, we must provide values for all the required fields. We will compose and send an HTTP request to try to update an existing toy, and we will fail to do so because we will just provide a value for the name. As in the previous request, we will use the value assigned to pk in the last toy we added:

    http PUT :8000/toys/4 name="PvZ 4 puzzle"

The following is the equivalent curl command:

    curl -iX PUT -H "Content-Type: application/json" -d '{"name":"PvZ 4   
puzzle"}' localhost:8000/toys/4

The previous commands will compose and send the following HTTP request: PUT http://localhost:8000/toys/4 with the following JSON key-value pair:

{  
   "name": "PvZ 4 puzzle",  
} 

The request will execute the same code we explained for the previous request. As we didn't provide all the required values for a Toy instance, the toy_serializer.is_valid() method will return False and the function will return an HTTP 400 Bad Request status code and the details generated in the toy_serializer.errors attribute serialized to JSON in the response body. The following lines show an example response for the HTTP request, with the required fields that our request didn't include values in the JSON response (description, release_date, and toy_category):

    HTTP/1.0 400 Bad Request
    Content-Length: 129
    Content-Type: application/json
    Date: Tue, 10 Oct 2017 17:23:46 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    X-Frame-Options: SAMEORIGIN
    
    {
        "description": [
            "This field is required."
        ], 
        "release_date": [
            "This field is required."
        ], 
        "toy_category": [
            "This field is required."
        ]
    }

When we want our API to be able to update a single field for an existing resource, in this case, an existing toy, we should provide an implementation for the PATCH method. The PUT method is meant to replace an entire resource and the PATCH method is meant to apply a delta to an existing resource. We can write code in the handler for the PUT method to apply a delta to an existing resource, but it is a better practice to use the PATCH method for this specific task. We will work with the PATCH method later.

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

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