We will start with command-line tools. One of the key advantages of command-line tools is that we can easily run again the HTTP requests after we build them for the first time, and we don't need to use the mouse or to tap the screen to run requests. We can also easily build a script with batch requests and run them. As happens with any command-line tool, it can take more time to perform the first requests compared with GUI tools, but it becomes easier once we've performed many requests and we can easily reuse the commands we have written in the past to compose new requests.
Curl, also known as cURL, is a very popular open source command-line tool and library that allows us to easily transfer data. We can use the curl command-line tool to easily compose and send HTTP requests and check their responses.
In macOS or Linux, you can open a Terminal and start using curl from the command line.
In Windows, you can work with curl in the Command Prompt or you can install curl as part of the Cygwin package installation option and execute it from the Cygwin terminal. In case you decide to use the curl command within the Command Prompt, download and unzip the latest version at http://curl.haxx.se/download.html. Then, make sure you include the folder in which the curl.exe file is included in your path to make it easy to run the command.
You can read more about the Cygwin terminal and its installation procedure at http://cygwin.com/install.html. In case you decide to use the Cygwin terminal, use it whenever you have to run the curl command instead of working with the Command Prompt.
Notice that Windows PowerShell includes the curl alias that calls the Inovoke-WebRequest command. Thus, in case you decide to work with Windows PowerShell, you will have to remove the curl alias to use the curl utility we use in this book.
We used the requirements.txt file to install the packages for our virtual environment. In this file, we specified httpie as one of the required packages. This way, we installed HTTPie, a command-line HTTP client written in Python that makes it easy to send HTTP requests and uses a syntax that is easier than curl. One of the great advantages of HTTPie is that it displays colorized output and uses multiple lines to display the response details. Thus, HTTPie makes it easier to understand the responses than the curl utility. However, it is very important to mention that HTTPie is slower than curl.
Whenever we compose HTTP requests with the command line, we will use two versions of the same command: the first one with HTTPie, and the second one with curl. This way, you will be able to use the one that is most convenient for you.
Make sure you leave the Flask development server running. Don't close the Terminal or Command Prompt that is running this development server. Open a new Terminal in macOS or Linux, or a Command Prompt in Windows, and run the following command. It is very important that you enter the ending slash (/) when specified because /service/notifications won't match any of the configured URL routes. Thus, we must enter /service/notifications/, including the ending slash (/). We will compose and send an HTTP request to create a new notification. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd01.txt file:
http POST ":5000/service/notifications/" message='eSports competition starts in 2 minutes' ttl=20 notification_category='Information'
The following is the equivalent curl command. It is very important to use the -H "Content-Type: application/json" option to tell curl to send the data specified after the -d option as application/json instead of the default application/x-www-form-urlencoded option.
The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd02.txt file:
curl -iX POST -H "Content-Type: application/json" -d '{"message":"eSports competition starts in 2 minutes", "ttl":20, "notification_category": "Information"}' "localhost:5000/service/notifications/"
The previous commands will compose and send the POST http://localhost:5000/service/notifications/ HTTP request with the following JSON key-value pairs:
{
"message": "eSports competition starts in 2 minutes",
"ttl": 20,
"notification_category": "Information"
}
The request specifies /service/notifications/ and, therefore, it will match '/service/notifications/' and run the NotificationList.post method. The method doesn't receive arguments because the URL route doesn't include any parameters. As the HTTP verb for the request is POST, Flask calls the post method. If the new NotificationModel was successfully persisted in the dictionary, the function returns an HTTP 201 Created status code and the recently persisted NotificationModel serialized to JSON in the response body. The following lines show an example response for the HTTP request, with the new NotificationModel object in the JSON response:
HTTP/1.0 201 CREATED
Content-Length: 283
Content-Type: application/json
Date: Wed, 10 Oct 2018 01:01:44 GMT
Server: Werkzeug/0.14.1 Python/3.7.1
{
"creation_date": "Wed, 10 Oct 2018 01:01:44 -0000",
"displayed_once": false,
"displayed_times": 0,
"id": 1,
"message": "eSports competition starts in 2 minutes",
"notification_category": "Information",
"ttl": 20,
"uri": "/service/notifications/1"
}
We will compose and send an HTTP request to create another notification. Go back to the Command Prompt in Windows, or the Terminal in macOS or Linux, and run the following command. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd03.txt file:
http POST ":5000/service/notifications/" message='Ambient temperature is above the valid range' ttl=15 notification_category='Warning'
The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd04.txt file:
curl -iX POST -H "Content-Type: application/json" -d '{"message":"Ambient temperature is above the valid range", "ttl":15, "notification_category": "Warning"}' "localhost:5000/service/notifications/"
The previous commands will compose and send the POST http://localhost:5000/service/notifications/ HTTP request with the following JSON key-value pairs:
{
"message": "Ambient temperature is above the valid range",
"ttl": 15,
"notification_category": "Warning"
}
The following lines show an example response for the HTTP request, with the new NotificationModel object in the JSON response:
HTTP/1.0 201 CREATED
Content-Length: 280
Content-Type: application/json
Date: Wed, 10 Oct 2018 21:07:40 GMT
Server: Werkzeug/0.14.1 Python/3.7.1
{
"creation_date": "Wed, 10 Oct 2018 21:07:40 -0000",
"displayed_once": false,
"displayed_times": 0,
"id": 2,
"message": "Ambient temperature is above valid range",
"notification_category": "Warning",
"ttl": 15,
"uri": "/service/notifications/2"
}
We will compose and send an HTTP request to retrieve all the notifications. Go back to the Command Prompt in Windows, or the Terminal in macOS or Linux, and run the following command. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd05.txt file:
http ":5000/service/notifications/"
The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd06.txt file:
curl -iX GET "localhost:5000/service/notifications/"
The previous commands will compose and send the GET http://localhost:5000/service/notifications/ HTTP request. The request specifies /service/notifications/ and, therefore, it will match '/service/notifications/' and run the NotificationList.get method. The method doesn't receive arguments because the URL route doesn't include any parameters. As the HTTP verb for the request is GET, Flask calls the get method. The method retrieves all the NotificationModel objects and generates a JSON response with all of these NotificationModel objects serialized.
The following lines show an example response for the HTTP request. The first lines show the HTTP response headers, including the status (200 OK) and the content type (application/json). After the HTTP response headers, we can see the details for the two NotificationModel objects in the JSON response:
HTTP/1.0 200 OK
Content-Length: 648
Content-Type: application/json
Date: Wed, 10 Oct 2018 21:09:43 GMT
Server: Werkzeug/0.14.1 Python/3.7.1
[
{
"creation_date": "Wed, 10 Oct 2018 21:07:31 -0000",
"displayed_once": false,
"displayed_times": 0,
"id": 1,
"message": "eSports competition starts in 2 minutes",
"notification_category": "Information",
"ttl": 20,
"uri": "/service/notifications/1"
},
{
"creation_date": "Wed, 10 Oct 2018 21:07:40 -0000",
"displayed_once": false,
"displayed_times": 0,
"id": 2,
"message": "Ambient temperature is above valid range",
"notification_category": "Warning",
"ttl": 15,
"uri": "/service/notifications/2"
}
]
After we run the three requests, we will see the following lines in the window that is running the Flask development server. The output indicates that the service received three HTTP requests, specifically two POST requests and one GET request with /service/notifications/ as the URI. The service processed the three HTTP requests, and returned the 201 status code for the first two requests and 200 for the last request:
127.0.0.1 - - [10/Oct/2018 18:07:31] "POST /service/notifications/ HTTP/1.1" 201 -
127.0.0.1 - - [10/Oct/2018 18:07:40] "POST /service/notifications/ HTTP/1.1" 201 -
127.0.0.1 - - [10/Oct/2018 18:09:43] "GET /service/notifications/ HTTP/1.1" 200 -
The following screenshot shows two Terminal windows side by side on macOS. The Terminal window on the left-hand side is running the Flask development server and displays the received and processed HTTP requests. The Terminal window on the right-hand side is running http commands to generate the HTTP requests. It is a good idea to use a similar configuration to check the output while we compose and send the HTTP requests:
Now, we will compose and send an HTTP request to retrieve a notification that doesn't exist. For example, in the previous list, there is no notification with an id value equal to 78. Run the following command to try to retrieve this notification. Make sure you use an id value that doesn't exist. We must make sure that the utilities display the headers as part of the response to see the returned status code. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd07.txt file:
http ":5000/service/notifications/78"
The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd08.txt file:
curl -iX GET "localhost:5000/service/notifications/78"
The previous commands will compose and send the GET http://localhost:5000/service/notifications/78 HTTP request. The request is the same as the previous one we analyzed, with a different number for the id parameter. The service will run the Notification.get method, with 78 as the value for the id argument. The method will execute the code that retrieves the NotificationModel object whose ID matches the id value received as an argument. However, the first line in the NotificationList.get method calls the abort_if_notification_not_found method, which won't find the ID in the dictionary keys, and it will call the flask_restful.abort function because there is no notification with the specified id 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 and the message included in the body. In this case, we just leave the default message. Of course, we can customize it based on our specific needs:
HTTP/1.0 404 NOT FOUND
Content-Length: 155
Content-Type: application/json
Date: Wed, 10 Oct 2018 21:24:32 GMT
Server: Werkzeug/0.14.1 Python/3.7.1
{
"message": "Notification 78 not found. You have requested this
URI [/service/notifications/78] but did you mean
/service/notifications/<int:id> ?"
}
We provide an implementation for the PATCH method to make it possible for our API to update a single field for an existing resource. For example, we can use the PATCH method to update two fields for an existing notification and set the value for its displayed_once field to true and displayed_times to 1. We don't want to use the PUT method because this method is meant to replace an entire notification.
The PATCH method is meant to apply a delta to an existing notification and, therefore, it is the appropriate method to just change the value of the displayed_once and displayed_times fields.
Now, we will compose and send an HTTP request to update an existing notification, specifically, to update the value of two fields. Make sure you replace 2 with the ID of an existing notification in your configuration. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd09.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_01_02 folder, in the Flask01/cmd10.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 specified JSON key-value pairs. The request has a number after /service/notifications/ and, therefore, it will match '/service/notifications/<int:id>' and run the Notification.patch method, that is, the patch method for the Notification class. If a NotificationModel instance with the specified ID exists and was successfully updated, the call to the method will return an HTTP 200 OK status code and the recently updated NotificationModel instance serialized to JSON in the response body. The following lines show a sample response:
HTTP/1.0 200 OK
Content-Length: 279
Content-Type: application/json
Date: Thu, 11 Oct 2018 02:15:13 GMT
Server: Werkzeug/0.14.1 Python/3.7.1
{
"creation_date": "Thu, 11 Oct 2018 02:15:05 -0000",
"displayed_once": true,
"displayed_times": 1,
"id": 2,
"message": "Ambient temperature is above valid range",
"notification_category": "Warning",
"ttl": 15,
"uri": "/service/notifications/2"
}
The IoT device will execute the previously explained HTTP request when it displays the notification for the first time. Then, it will make additional PATCH requests to update the value for the displayed_times field.
Now, we will compose and send an HTTP request to delete an existing notification, specifically, the last one we added. As happened in our last HTTP requests, we have to check the value assigned to id in the previous response and replace 2 in the command with the returned value. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd11.txt file:
http DELETE ":5000/service/notifications/2"
The following is the equivalent curl command. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/cmd12.txt file:
curl -iX DELETE "localhost:5000/service/notifications/2"
The previous commands will compose and send the DELETE http://localhost:5000/service/notifications/2 HTTP request. The request has a number after /service/notifications/ and, therefore, it will match '/service/notifications/<int:id>' and run the Notification.delete method, that is, the delete method for the Notification class. If a NotificationModel instance with the specified ID exists and was successfully deleted, the call to the method will return an HTTP 204 No Content status code. The following lines show a sample response:
HTTP/1.0 204 NO CONTENT
Content-Length: 3
Content-Type: application/json
Date: Thu, 11 Oct 2018 02:22:09 GMT
Server: Werkzeug/0.14.1 Python/3.7.1