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

ESP32 HTTP server: Multiple methods allowed on same route

DFRobot Mar 15 2018 757

The objective of this esp32 tutorial is to explain how we can allow multiple HTTP methods on the same route of an async HTTP web server running on the ESP32, using the Arduino core. The tests of this tutorial were performed using a DFRobot’s ESP32 module device integrated in a ESP32 development board.

Introduction

The objective of this esp32 tutorial is to explain how we can allow multiple HTTP methods on the same route of an async HTTP web server running on the ESP32, using the Arduino core.

In this previous post we saw how we could control the HTTP methods allowed on a route, but we only had the option to allow a specific method per route or all methods.

Nonetheless, for some use cases, it could be useful to allow some methods, but not all (for example, allowing GET and POST requests on a given route but not PUT requests).

As mentioned in the previous tutorial, we control the HTTP methods allowed in a route by passing a specific enumerated value to the on method. That enum is specified here and as can be seen by its values it follows a bit flag pattern.

This means that allowing a specific method corresponds to setting a given bit of a byte to true. In the case of the HTTP_ANY value, all the bits are set to 1, which is a good indicator that we can actually mix different methods by setting their bits to 1. We will test this in our code.
The tests of this tutorial were performed using a DFRobot’s ESP32 module device integrated in a ESP32 development board.

The code

The first part of the code follows the pattern we have been using in previous tutorials. We start by the includes, followed by declaring the credentials of the WiFi network as global variables.We will also need an object of class AsyncWebServer, which will also be stored on a global variable.

#include "WiFi.h"
#include "FS.h"
#include "AsyncTCP.h"
#include "ESPAsyncWebServer.h"
 
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPassword";
 
AsyncWebServer server(80);

In the Arduino setup, we open, a serial connection and then we call the functions needed to connect the ESP32 to the wireless network.

Serial.begin(115200);
 
WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
}
 
Serial.println(WiFi.localIP());

Now we will handle the route configuration by calling the on method on the server object. We will call this route “/test”.

We will configure our route to accept both HTTP GET and POST requests. Thus, as second argument of the on method, we need to pass a byte with the corresponding bit flags of these methods set to 1 and the remaining to 0.

We can set the final value using the bitwise OR operator “|” between the enum values for the GET and POST requests.

As response, we will return a simple “ok” message to the client.

server.on("/test", HTTP_POST | HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "ok");
});

To finalize, we need to call the begin method on the server object so it starts listening to incoming requests. The final source code is shown below.

#include "WiFi.h"
#include "FS.h"
#include "AsyncTCP.h"
#include "ESPAsyncWebServer.h"
 
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPassword";
 
AsyncWebServer server(80);
 
void setup(){
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println(WiFi.localIP());
 
  server.on("/test", HTTP_POST | HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "ok");
  });
 
  server.begin();
}
 
void loop(){}

Testing the code

The testing procedure will be the same we have been covering on previous posts. After compiling and uploading the code, simply open the serial monitor and copy the IP printed.

Next, open Postman to start sending the requests to the route we have configured in the code. We will try different HTTP methods, but the URL will always be the following (just change {yourDeviceIP} by the IP you have previously copied):

http://{yourDeviceIp}/test

We will start my making a HTTP GET request, by choosing that method from the methods dropdown of Postman. As shown in figure 1, upon sending the request, you should get the “ok” message we have defined as response, indicating that the request was successfully received by the server.


Figure 1 – Result for the HTTP GET method request.

Next we will try the HTTP POST method. Note that we don’t need to define any body data, since our aim is just to check if the request is received by the server and correctly processed.
Upon sending the request, you should get an output like figure 2, also indicating success.


Figure 2 – Result for the HTTP POST method request.


To finalize we will send an PUT request. As shown in figure 3, for this case, the server will not process the request and give a 500 error code, since this method is not allowed on the route.

Figure 3 – Result for the HTTP PUT method request.

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



REVIEW