Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On RESTful Python Web Services

You're reading from   Hands-On RESTful Python Web Services Develop RESTful web services or APIs with modern Python 3.7

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher
ISBN-13 9781789532227
Length 500 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Developing RESTful APIs and Microservices with Flask 1.0.2 FREE CHAPTER 2. Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask 3. Improving Our API and Adding Authentication to it with Flask 4. Testing and Deploying an API in a Microservice with Flask 5. Developing RESTful APIs with Django 2.1 6. Working with Class-Based Views and Hyperlinked APIs in Django 2.1 7. Improving Our API and Adding Authentication to it with Django 8. Throttling, Filtering, Testing, and Deploying an API with Django 2.1 9. Developing RESTful APIs with Pyramid 1.10 10. Developing RESTful APIs with Tornado 5.1.1 11. Working with Asynchronous Code, Testing, and Deploying an API with Tornado 12. Assessment 13. Other Books You May Enjoy

Making HTTP requests to the Flask API

Now, we can run the service/service.py script that launches Flask's development server to compose and send HTTP requests to our unsecured and simple web API (we will definitely add security later). Execute the following command. Make sure you have the virtual environment activated:

    python service/service.py

The following lines show the output after we execute the previous command. The development server is listening at port 5000:

     * Serving Flask app "service" (lazy loading)
     * Environment: production
       WARNING: Do not use the development server in a production environment.
       Use a production WSGI server instead.
     * Debug mode: on
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger PIN: 122-606-712
  

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.

If we specify 0.0.0.0 as the desired IP address for IPv4 configurations, the development server will listen on every interface on port 5000. In addition, it is necessary to open the default port 5000 in our firewalls (software and/or hardware) and configure port forwarding to the computer that is running the development server. The same configuration applies whenever we want to run the application on any cloud provider.

We just need to specify '0.0.0.0' as the value for the host argument in the call to the app.run method, specifically, the last line in the service/service.py file. The following line shows the new call to app.run, which launches Flask's development server in an IPv4 configuration and allows requests to be made from other computers and devices connected to our LAN. The line generates an externally visible server. The code file for the sample is included in the restful_python_2_01_02 folder, in the Flask01/service/service.py file:

if __name__ == '__main__': 
    app.run(host='0.0.0.0', debug=True) 
If you decide to compose and send HTTP requests from other computers or devices connected to the LAN, remember that you have to use the development computer's assigned IP address instead of localhost. For example, if the computer's assigned IPv4 IP address is 192.168.1.127, instead of localhost:5000, you should use 192.168.1.127:5000. Of course, you can also use the hostname instead of the IP address. The configurations explained previously are very important because mobile devices might be the consumers of our RESTful APIs and future microservices. We will always want to test the apps that make use of our APIs in our development environments. In addition, we can work with useful tools, such as ngrok, that allow us to generate secure tunnels to localhost. You can read more about ngrok at http://www.ngrok.com.

The Flask development server is running on localhost (127.0.0.1), listening on port 5000, and waiting for our HTTP requests. Now, we will compose and send HTTP requests locally in our development computer or from other computers or devices connected to our LAN.

Throughout this book, we will use the following tools to compose and send HTTP requests:

  • Command-line tools
  • GUI tools
  • Python code
  • The web browser
Notice that you can use any other application that allows you to compose and send HTTP requests. There are many apps that run on tablets and smartphones that allow you to accomplish this task. However, we will focus our attention on the most useful tools when building RESTful Web APIs and microservices.

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.

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

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.

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:

  1. 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.
  2. 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.
  3. Click Body in the right-hand side of the Authorization and Headers tabs, within the panel that composes the request.
  4. 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.
  5. 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:

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:

  1. Select File | New | HTTP Request in the main menu.
  2. 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.
  1. 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 explained previously 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:

Consuming the API with other programming languages

We've built our first RESTful Web Service that is capable of running as a microservice with Flask and Python. We can consume the API with any modern programming language that can compose and send HTTP requests to the resources and verbs supported by the API and work easily with JSON content.

It is extremely important to make sure that we set the content type for the HTTP request as we did when working with the curl and http command-line utilities. We just need to check which is the most convenient way of doing so in the programming language that we have to use as a client.

The fact that we can easily run a Flask development server and check its console output whenever a new request is processed makes it easy to check which requests arrive at the server. In this case, we are working with a basic and unsecured API. However, we will work with secure and more advanced APIs in the forthcoming chapters.

You have been reading a chapter from
Hands-On RESTful Python Web Services - Second Edition
Published in: Dec 2018
Publisher:
ISBN-13: 9781789532227
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image