• No results found

Composing and sending HTTP requests

In document Internet of Things With Python (Page 121-126)

The HTTP server is running in Yocto Linux and waiting for our HTTP requests to control the LEDs on connected to the Intel Galileo Gen 2 board. Now, we will compose and send HTTP requests locally in Yocto Linux and then from other computer or devices connected to our LAN.

HTTPie supports curl-like shorthands for localhost. For example, :8888 is a shorthand that expands to http://localhost:8888. We already have an SSH terminal running the HTTP server, and therefore, we can run the following command in another SSH terminal.

http GET :8888/version

Chapter 4 The previous command will compose and send the following HTTP request:

GET http://localhost:8888/version. The request is the simplest case in our RESTful API because it will match and run the VersionHandler.get method that just receives self as a parameter because the URL pattern doesn't include any parameters. The method creates a response dictionary and then calls the self.write method with response as a parameter. The self.write method writes the received chunk to the output buffer. Because the chunk (response) is a dictionary, self.

write writes it as JSON and sets the Content-Type of the response to application/

json. The following lines show an example response for the HTTP request, including the response headers:

HTTP/1.1 200 OK Content-Length: 46

Content-Type: application/json; charset=UTF-8 Date: Thu, 28 Jan 2016 03:15:21 GMT

Etag: "fb066668a345b0637fdc112ac0ddc37c318d8709"

Server: TornadoServer/4.3 {

"last_build": "2016-01-28", "version": "1.0"

}

We can execute HTTPie with the -b option in case we don't want to include the header in the response. For example, the following line performs the same HTTP request but doesn't display the header in the response output.

http –b GET :8888/version

Once we know that our request is running OK, we can open a new terminal, command-line or the GUI tool that we want to use to compose and send HTTP requests from a computer or any device connected to the LAN. We just need to use the IP address assigned to the board instead of localhost in our request URLs. Don't forget to replace 192.168.1.107 with your board's IP address in the next requests.

Now, we can run the following HTTPie command in a computer or device to use the RESTful API to make the board turn on the five LEDs. After you enter the command, you will notice the SSH terminal that displays the output for the Python code will display a message indicating that it is turning on 5 LEDs and the additional messages indicating the LEDs that are being turned on and off. In addition, you will see 5 LEDs turned on.

http -b PUT 192.168.1.107:8888/putnumberinleds/5

The previous command will compose and send the following HTTP request: PUT http://192.168.1.107:8888/putnumberinleds/5. The request will match and run the PutNumberInLedsHandler.put method that receives 5 in its number parameter.

The following lines show the response from the HTTP server with the number that has been printed in LEDs, that is, the number of LEDs that have been turned on:

{

"number": 5 }

The following image shows two Terminal windows side-by-side on OS X. The Terminal window at the left-hand side is running on a computer that is generating the HTTP requests and the Terminal window at the right-hand side is the SSH terminal that is running the Tornado HTTP server in Yocto Linux and displays the output for our Python code. It is a good idea to use a similar configuration to check the output while we compose and send the HTTP requests.

In Fiddler, click Composer or press F9, select PUT in the dropdown menu in the Parsed tab, and enter 192.168.1.107:8888/putnumberinleds/5 in the textbox at the right-hand side of the dropdown (don't forget to replace the IP with your board's IP). Then, click Execute and double-click on the 200 result that appears on the capture log. If you want to see the raw response, just click on the Raw button below the Request Headers panel.

Chapter 4 The following image shows a Fiddler window side-by-side with a Putty terminal window on Windows. The Fiddler window at the left-hand side is running on a computer that is generating the HTTP requests and the Putty terminal window at the right-hand side is the SSH terminal that is running the Tornado HTTP server in Yocto Linux and displays the output for our Python code.

We can run the following HTTPie command in a computer or device to use the RESTful API to tell us how many LEDs are turned on.

http -b GET 192.168.1.107:8888/getcurrentnumber

The previous command will compose and send the following HTTP request: GET http://192.168.1.107:8888/getcurrentnumber. The request will match and run the GetCurrentNumber.get method. The following lines show the response from the HTTP server with the number that has been printed in LEDs, that is, the number of LEDs that have been turned on with the last API call:

{

"number": 5 }

If we take a look again at the list of request handlers that make up the web application, we will notice that the entry for putnumberinleds specifies a regular expression that accepts numbers from 0 to 9 as its parameters:

(r"/putnumberinleds/([0-9])", PutNumberInLedsHandler)

If we run the following HTTPie command in a computer or device to use the RESTful API to make the board turn on twelve LEDs, the request won't match any regular expression in the list of request handlers.

http -b PUT 192.168.1.107:8888/putnumberinleds/12 Thus, Tornado will return a 404: Not found error as a result.

<html><title>404: Not Found</title><body>404: Not Found</body></html>

The same will happen if we run the following HTTPie command in a computer or device because x isn't a number between 0 and 9.

http -b PUT 192.168.1.107:8888/putnumberinleds/x The following HTTPie command will turn on 8 LEDs.

http -b PUT 192.168.1.107:8888/putnumberinleds/8

Chapter 4 The previous command will compose and send the following HTTP request: PUT http://192.168.1.107:8888/putnumberinleds/8. The request will match and run the PutNumberInLedsHandler.put method that receives 8 in its number parameter.

The following lines show the response from the HTTP server with the number that has been printed in LEDs, that is, the number of LEDs that have been turned on:

{

"number": 8 }

The number of LEDs that are turned on changed from 5 to 8, and therefore, we can run the following HTTPie command in a computer or device to use the RESTful API to tell us how many LEDs are turned on.

http -b GET 192.168.1.107:8888/getcurrentnumber

The following lines show the response from the HTTP server with the number that has been printed in LEDs:

{

"number": 8 }

We created a very simple RESTful API that allows us to turn on LEDs and check which is the number that is currently printed in LEDs. Of course, we should add authentication and overall security to the RESTful API in order to make it complete. Our RESTful API makes it possible for us to print numbers in LEDs with any application, mobile app or web application that can compose and send HTTP requests.

In document Internet of Things With Python (Page 121-126)