The following lines show the output after we execute the previous command. The development server is listening at port 5000
:
With the previous command, we will start the Flask development server and we will only be able to access it in our development computer. The previous command starts the development server in the default IP address, that is, 127.0.0.1
(localhost). It is not possible to access this IP address from other computers or devices connected to our LAN. Thus, if we want to make HTTP requests to our API from other computers or devices connected to our LAN, we should use the development computer IP address, 0.0.0.0
(for IPv4 configurations) or ::
(for IPv6 configurations) as the desired IP address for our development server.
Working with the curl and httpie command-line tools
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.
Note
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
.
Note
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 CREATEDContent-Length: 283Content-Type: application/jsonDate: Wed, 10 Oct 2018 01:01:44 GMTServer: 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 CREATEDContent-Length: 280Content-Type: application/jsonDate: Wed, 10 Oct 2018 21:07:40 GMTServer: 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 OKContent-Length: 648Content-Type: application/jsonDate: Wed, 10 Oct 2018 21:09:43 GMTServer: 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 FOUNDContent-Length: 155Content-Type: application/jsonDate: Wed, 10 Oct 2018 21:24:32 GMTServer: 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.
Note
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"
}
Note
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 CONTENTContent-Length: 3Content-Type: application/jsonDate: Thu, 11 Oct 2018 02:22:09 GMTServer: Werkzeug/0.14.1 Python/3.7.1
Working with GUI tools – Postman and others
So far, we have been working with two terminal-based, or command-line, tools to compose and send HTTP requests to our Flask development server: cURL and HTTPie. Now, we will work with a GUI (short for Graphical User Interface) tool.
Postman is a very popular API testing suite GUI tool that allows us to easily compose and send HTTP requests, among other features. Postman is available as a Chrome App and as a Macintosh App. We can execute it in Windows, Linux, and macOS as a native app. You can download the versions of the Postman app at https://www.getpostman.com/apps.
Note
You can download and install Postman for free to compose and send HTTP requests to our RESTful APIs. You just need to sign up to Postman; we won't be using any of the paid features provided by Postman in our examples. All the instructions work with Postman 6.4.2 or higher.
Now, we will compose and send HTTP requests to localhost:5000
and test the RESTful API with this GUI tool. Postman doesn't support shorthand for localhost and, therefore, we cannot use the same shorthand we have been using when composing requests with HTTPie.
Once you launch Postman, make sure you close the modal that provides shortcuts to common tasks. Select GET
request in the +
new drop-down menu in the upper-left corner of the Postman main window.
Select GET
in the drop-down menu on the left-hand side of the Enter request URL
textbox, and then enter localhost:5000/service/notifications/
in this textbox on the right-hand side of the dropdown.
Then, click Send
and Postman will display the following information:
Status
: 200 OK
.Time
: The time it took for the request to be processed.Size
: The response size calculated by adding the body size to the header's size.Body
: The response body with all the notifications formatted as JSON with syntax highlighting. The default view for the response body is the Pretty view, and it activates syntax highlighting that makes it easy to read JSON code.
The following screenshot shows the JSON response body in Postman for the HTTP GET
request to localhost:5000/service/notifications/
:
Click on the Headers
tab on the right-hand side of the Body
and Cookies
tabs to read the response headers. The following screenshot shows the layout for the response headers that Postman displays for the previous response. Notice that Postman displays the Status
in the right-hand side of the response and doesn't include it as the first line of the headers, as happened when we worked with both the curl
and http
command-line utilities:
Now, we will compose and send an HTTP request to create a new notification, specifically, a POST
request. Follow these steps:
- Click on the plus (
+
) button on the right-hand side of the tab that showed the previous request. This is to create a new tab. - Select
POST
in the drop-down menu on the left-hand side of the Enter request URL
textbox, and enter localhost:5000/service/notifications/
in the textbox in the right-hand side of the dropdown. - Click
Body
in the right-hand side of the Authorization
and Headers
tabs, within the panel that composes the request. - Activate the raw radio button and select
JSON
(application/json
) in the dropdown on the right-hand side of the binary radio button. Postman will automatically add a Content-type = application/json
header and, therefore, you will notice the Headers
tab will be renamed to Headers (1)
, indicating that there is one key-value pair specified for the request headers. - Enter the following lines in the textbox under the radio buttons, within the
Body
tab. The code file for the sample is included in the restful_python_2_01_02
folder, in the Flask01/cmd13.txt
file:
{
"message": "Calculating the most appropriate ambient temperature",
"ttl": 20,
"notification_category": "Warning"
}
The following screenshot shows the request body in Postman:
We followed the necessary steps to create an HTTP POST
request with a JSON body that specifies the necessary key-value pairs to create a new notification. Click Send
and Postman will display the following information:
Status
: 201 Created.
Time
: The time it took for the request to be processed.Size
: The response size calculated by adding the body size to the header's size.Body
: The response body, with the recently added notification formatted as JSON with syntax highlighting (Pretty view).
The following screenshot shows the JSON response body in Postman for the HTTP POST
request:
Note
If we want to compose and send an HTTP PATCH
request for our API with Postman, it is necessary to follow the steps explained previously to provide JSON data within the request body.
Click or tap on the value for the uri
field in the JSON response body: /service/notifications/3
. You will notice that the value will be underlined when you hover the mouse over it. Postman will automatically generate a GET
request to localhost:5000/service/notifications/3
, as shown in the following screenshot:
Click Send
to run it and retrieve the recently added notification. Notice that the uri
field is useful for browsing the API with a tool such as Postman.
One of the nice features included in Postman is that we can easily review and re-run the HTTP requests we have made by browsing the saved History
shown on the left-hand side of the Postman window. The History
panel displays a list with the HTTP verb, followed by the URL for each HTTP request we have composed and sent. We just need to click on the desired HTTP request and click Send
to run it again. The following screenshot shows the many HTTP requests in the History
panel with the first one selected to send it again:
JetBrains PyCharm is a very popular multiplatform Python IDE (short for Integrated Development Environment) available on macOS, Linux, and Windows. Its paid professional version includes a REST Client that allows us to easily test RESTful Web Services and microservices. In case we work with this version of the IDE, we can compose and send HTTP requests without leaving the IDE. You don't need a JetBrains PyCharm Professional version license to run the examples included in this book. You can take advantage of the free 30-day trial. However, in case you don't want to install this IDE, you can skip the steps and use the provided http
or curl
commands, which perform the same task. Because the IDE is very popular, we will learn the necessary steps to compose and send an HTTP request for our API by using the HTTP client included in the editor that replaced the deprecated REST Client.
Now, we will use the HTTP client included in PyCharm Professional to compose and send an HTTP request to create a new game, specifically, a POST
request. Follow these steps:
- Select
File
| New
| HTTP Request
in the main menu. - Enter
notifications_post_pycharm
in the Name
textbox and click OK
. The IDE will create a new file with the http
extension and with instructions on how to build HTTP requests.
- Replace the code with the following lines. The code starts with the HTTP method name,
POST
, followed by the URL. The following line specifies the header with the value for Content-Type
and the next lines provide the JSON body within curly brackets. The code file for the sample is included in the restful_python_2_01_02
folder, in the Flask01/notifications_post_pycharm.http
file:
POST http://localhost:5000/service/notifications/
Content-Type: application/json
{
"message": "Working with PyCharm Professional",
"ttl": 12,
"notification_category": "Information"
}
The following screenshot shows the request incorporated in PyCharm Professional's editor:
We followed the necessary steps to create an HTTP POST
request with a JSON body that specifies the necessary key-value pairs to create a new notification.
Click the run HTTP request button, that is, the first button with the play icon in the upper-left corner of the editor, under the tab's name (notifications_post_1.http
). Select Run
localhost:5000
in the context menu that is displayed.
PyCharm will compose and send the HTTP POST
request, which will activate the Run
tab and display the request we made, the response headers, the response body, the response code 201 (Created)
, the time it took for the request to be processed, and the content length at the bottom of the output. By default, PyCharm will automatically apply JSON syntax highlighting to the response. The following screenshot shows the output in the Run
tab for the HTTP POST
request:
In case you don't want to work with PyCharm Professional, run any of the following commands to compose and send the HTTP POST
request to create the new notification. The code file for the sample is included in the restful_python_2_01_02
folder, in the Flask01/cmd14.txt
file:
http POST ":5000/service/notifications/" message='Working with
PyCharm Professional' ttl=12 notification_category='Information'
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/cmd15.txt
file:
curl -iX POST -H "Content-Type: application/json" -d '{"message":"Working with PyCharm Professional", "ttl":12, "notification_category": "Information"}' "localhost:5000/service/notifications/"
Because we made the necessary changes to generate an externally visible Flask development server, we can also use apps that can compose and send HTTP requests from mobile devices to work with the RESTful API.
For example, we can work with the iCurlHTTP App on iOS devices such as iPad Pro, iPad, and iPhone: https://itunes.apple.com/us/app/icurlhttp/id611943891?mt=8. In Android devices, we can work with the HTTP Request App: https://play.google.com/store/apps/details?id=air.http.request&hl=en.
The next screenshot shows the results of composing and sending the following HTTP request with the GET http://192.168.1.106:8000/service/notitications/
iCurlHTTP app. Remember that you have to perform the configurations explainedpreviously in your LAN and router to be able to access the Flask development server from other devices connected to your LAN. In this case, the IP assigned to the computer running the Flask development server is 192.168.1.106
and, therefore, you must replace this IP with the IP assigned to your development computer. At the time this book was published, the mobile apps that allow you to compose and send HTTP requests do not provide all the features you can find in Postman or command-line utilities: