As we explained in Chapter 6
, Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask, our API is able to update a single field for an existing resource, and therefore, we provide an implementation for the PATCH
method. For example, we can use the PATCH
method to update an existing message and set the value for its printed_once
and printed_times
fields to true
and 1
. We don't want to use the PUT
method because this method is meant to replace an entire message. The PATCH
method is meant to apply a delta to an existing message, and therefore, it is the appropriate method to just change the value of those two fields.
Now, we will compose and send an HTTP request to update an existing message, specifically, to update the value of the printed_once
and printed_times
fields. Because we just want to update two fields, we will use the PATCH
method instead of PUT
. Make sure you replace 1
with the id or primary key of an existing message in your configuration:
http PATCH :5000/api/messages/1 printed_once=true printed_times=1
The following is the equivalent curl
command:
curl -iX PATCH -H "Content-Type: application/json" -d '{"printed_once":"true", "printed_times":1}' :5000/api/messages/1
The previous command will compose and send a PATCH
HTTP request with the following specified JSON key-value pairs:
{ "printed_once": true, "printed_times": 1 }
The request has a number after /api/messages/
, and therefore, it will match '/messages/<int:id>'
and run the MessageResource.patch
method, that is, the patch
method for the MessageResource
class. If a Message
instance with the specified id exists, the code will retrieve the values for the printed_times
and printed_once
keys in the request dictionary update the Message
instance and validate it.
If the updated Message
instance is valid, the code will persist the changes in the database and the call to the method will return an HTTP 200 OK
status code and the recently updated Message
instance serialized to JSON in the response body. The following lines show a sample response:
HTTP/1.0 200 OK Content-Length: 368 Content-Type: application/json Date: Tue, 09 Aug 2016 22:38:39 GMT Server: Werkzeug/0.11.10 Python/3.5.1 { "category": { "id": 1, "name": "Information", "url": "http://localhost:5000/api/categories/1" }, "creation_date": "2016-08-08T12:18:43.260474+00:00", "duration": 5, "id": 1, "message": "Checking temperature sensor", "printed_once": true, "printed_times": 1, "url": "http://localhost:5000/api/messages/1" }
We can run the commands explained in Chapter 6
,
Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask, to check the contents of the tables that the migrations created in the PostgreSQL database. We will notice that the printed_times
and printed_once
values have been updated for the row in the message table. The following screenshot shows the contents for the updated row of the message
table in a PostgreSQL database after running the HTTP request. The screenshot shows the results of executing the following SQL query: SELECT * FROM message WHERE id = 1
:
3.135.187.210