ESP32 Picoweb: Obtaining the HTTP Method of the request

The objective of this post is to explain how to obtain the HTTP method from a request performed to a MicroPython Picoweb application. The tests shown through this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


Introduction

The objective of this post is to explain how to obtain the HTTP method from a request performed to a MicroPython Picoweb application.

The tests shown through this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The code was developed on uPyCraft. You can check how to use uPyCraft in this previous post.

 

The code

As usual, we will start by importing the modules needed and connect the ESP32 to a WiFi network, so it can be reached from a web browser when testing. I’m including the WiFi connection code explicitly for simplicity, but it could be easily encapsulated in a function inside a module.

import picoweb
import network

ssid = "yourNetworkName"
password =  "yourPassword"

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

ip = station.ifconfig()

After finishing connecting to WiFi, we will create the Picoweb app instance and declare a route where we will get a request and check for the HTTP method. We will use the index route for this example.

@app.route("/")
def index(req, resp):
#Handling function code

Note that in the previously declared handling function we have two arguments that are automatically passed to the function by the framework. As we have seen in previous tutorials, the second argument is a stream writer that we use to send back the response to the client.

The first argument, which we haven’t used yet before, is an object of class HTTPRequest and makes available some information about the received request.

Thus, to get the HTTP method of the received request, we simply access the method attribute of the mentioned object. Note that we called the object req when we specified the arguments of the route handling function.

method = req.method
print("Method was:" + method)

Now that we know the method, we will build a simple logic on our route to return a HTTP method not allowed error when the method is POST and return some test information otherwise.

You can check how to return HTTP errors on this previous post. The method not allowed code corresponds to 405 [1].

if method == "POST":
   yield from picoweb.http_error(resp, "405")

else:
   yield from picoweb.start_response(resp)
   yield from resp.awrite("HTTP method was allowed")

Finally, we will call the run method to start our app. The final code for the script can be seen below.

import picoweb
import network

ssid = "yourNetworkName"
password =  "yourPassword"
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

ip = station.ifconfig()

app = picoweb.WebApp("myApp")

@app.route("/")
def index(req, resp):
    method = req.method

    print("Method was:" + method)

    if method == "POST":
      yield from picoweb.http_error(resp, "405")

    else:
      yield from picoweb.start_response(resp)
      yield from resp.awrite("HTTP method was allowed")

app.run(debug=True, host =ip[0])

 

Testing the code

To test the code, simply upload the script to your ESP32 board and run it. Upon execution, a URL should get printed to the console. To test the GET method, simply copy this URL and paste it on a web browser. You should get an output similar to figure 1, which shows the content we defined in the code being returned.

ESP32 Picoweb HTTP get method allowed output

Figure 1 – Output of the HTTP GET request performed to the ESP32 Picoweb app, via web browser.

For that request, if we check the output in the MicroPython prompt, the correct method was printed, as can be seen in figure 2.

ESP32 PicoWeb HTTP GET method command line output.png

Figure 2 – Output of the route handling function for a HTTP GET request.

In order to test sending a HTTP POST request, we can use a tool like Postman, which makes the process of sending HTTP requests very simple. You can check here a quick video explanation on how to use Postman for sending HTTP POST requests.

The output of sending a POST request with this tool is shown in figure 3. As can be seen, the returned HTTP code is 405, as expected.

Postman sending HTTP Post request to picoweb ESP32 app.png

Figure 3 – Output of the HTTP POST request, performed on Postman.

Finally, if we go back to the MicroPython console, we can check that also the correct HTTP method was printed, as illustrated on figure 4.

ESP32 Picoweb HTTP POST Request received.png

Figure 4 – Output of the route handling function for a HTTP POST request.

 

Related Posts

 

References

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

Advertisements
This entry was posted in ESP32 and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s