We have already installed HTTPie (https://httpie.org/) as part of the requirements.txt step. Although it does not show in black and white text, HTTPie has better syntax highlighting. It also has more intuitive command-line interaction with the RESTful HTTP server. We can use it to test our first Flask application (more explanation on HTTPie to follow):
$ http GET http://172.16.1.173:5000/
HTTP/1.0 200 OK
Content-Length: 17
Content-Type: text/html; charset=utf-8
Date: Wed, 22 Mar 2017 17:37:12 GMT
Server: Werkzeug/0.9.6 Python/3.5.2
Hello Networkers!
We will use HTTPie as our client for this chapter; it is worth a minute or two to take a look at its usage. We will use the HTTP-bin (https://httpbin.org/) service to show the use of HTTPie. The usage of HTTPie follows this simple pattern:
$ http [flags] [METHOD] URL [ITEM]
Following the preceding pattern, a GET request is very straightforward, as we have seen with our Flask development server:
$ http GET https://httpbin.org/user-agent
...
{
"user-agent": "HTTPie/0.8.0"
}
JSON is the default implicit content type for HTTPie. If your body contains just strings, no other operation is needed. If you need to apply non-string JSON fields, use := or other documented special characters:
$ http POST https://httpbin.org/post name=eric twitter=at_ericchou married:=true
HTTP/1.1 200 OK
...
Content-Type: application/json
...
{
"headers": {
...
"User-Agent": "HTTPie/0.8.0"
},
"json": {
"married": true,
"name": "eric",
"twitter": "at_ericchou"
},
...
"url": "https://httpbin.org/post"
}
As you can see, HTTPie improves the traditional curl syntax and makes testing the REST API a breeze.
Getting back to our Flask program, a large part of API building is based on the flow of URL routing. Let's take a deeper look at the app.route() decorator.