$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32MicroPythonESP8266

ESP32 Picoweb Tutorial: Obtaining the HTTP Method of the request

DFRobot Oct 13 2017 969

The objective of this ESP32 Picoweb Tutorial 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 ESP32 module device integrated in a ESP32 development board.

Introduction

The objective of this ESP32 Picoweb Tutorial is to explain how to obtain the HTTP method from a request performed to a MicroPython Picoweb application.
The tests shown through this ESP32 module tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 development 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.


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.


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.



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.

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

NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.

REVIEW