Updating fields for a resource with the PATCH method

Now we will compose and send an HTTP request to update an existing notification, specifically, to update the value of the displayed_once and displayed_times fields. Because we just want to update two fields, we will use the PATCH method instead of PUT. Make sure you replace 2 with the ID or primary key of an existing notification in your configuration. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd307.txt file:

http PATCH ":5000/service/notifications/2" displayed_once=true displayed_times=1

The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_03_01 folder, in the Flask01/cmd308.txt file:

curl -iX PATCH -H "Content-Type: application/json" -d '{"displayed_once":"true", "displayed_times":1}' "localhost:5000/service/notifications/2"
  

The previous command will compose and send a PATCH HTTP request with the following JSON key-value pairs:

{ 
    "displayed_once": true, 
    "displayed_times": 1 
}

The request has a number after /service/notifications/, and therefore, it will match '/notifications/<int:id>' and run the NotificationResource.patch method, that is, the patch method for the NotificationResource class. If a Notification instance with the specified ID exists, the code will retrieve the values for the displayed_times and displayed_once keys from the request dictionary, and will update and validate the Notification instance. If the updated Notification 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 Notification instance serialized to JSON in the response body. The following lines show a sample response:

    HTTP/1.0 200 OK
    Content-Length: 390
    Content-Type: application/json
    Date: Fri, 19 Oct 2018 04:02:04 GMT
    Server: Werkzeug/0.14.1 Python/3.7.1
    
    {
        "creation_date": "2018-10-16T21:58:43.737812+00:00",
        "displayed_once": true,
        "displayed_times": 1,
        "id": 2,
        "message": "No winners yet",
        "notification_category": {
            "id": 2,
            "name": "Warning",
            "url": 
"http://localhost:5000/service/notification_categories/2"
}, "ttl": 15, "url": "http://localhost:5000/service/notifications/2" }

We can run the commands explained in Chapter 2, Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask, to check the contents of the tables that the migrations process created in the PostgreSQL 10.5 database. We will notice that the displayed_times and displayed_once values have been updated for the row in the notification table.

The following command will allow you to check the contents for the updated row of the notification table in a PostgreSQL database after running the HTTP request. The next command runs the following SQL query: SELECT * FROM notification WHERE id = 2. The code file for the sample is included in the restful_python_2_02_01 folder, in the Flask01/check_patched_notification.sql file:

psql --username=your_user_name --dbname=flask_notifications --command="SELECT * FROM notification WHERE id = 2;"
  

The following screenshot shows the contents for the updated row of the notification table in a PostgreSQL database after running the HTTP request:

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

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