Home / References / ESP32 Library / WebServer Library
Description
The method()
method is a member function of the ESP32 WebServer
class. It is used within a request handler function to identify the HTTP method associated with the incoming client request. Knowing the method is crucial for processing requests correctly, as different methods imply different actions (e.g., GET for retrieving data, POST for submitting data).
Syntax and Usage
The method()
method is called on an instance of the WebServer
object, typically from within a function designated to handle incoming client requests.
server.method()
This is the standard way to use the method. It is called without any arguments and returns the HTTP method used in the request.// Assuming 'server' is your WebServer object instance HTTPMethod requestMethod = server.method(); // Example of checking the method type if (requestMethod == HTTP_POST) { // Handle POST request specifically } else if (requestMethod == HTTP_GET) { // Handle GET request specifically }
This syntax is typically used inside handler functions (registered viaserver.on()
,server.onNotFound()
, etc.) to apply different logic based on the request type.
Argument(s)
This method does not require any arguments.
Return Value
The method()
method returns a value of the enumeration type HTTPMethod
. This enumeration defines constants representing standard HTTP request methods. Common values include:
HTTP_GET
HTTP_POST
HTTP_PUT
HTTP_DELETE
HTTP_PATCH
HTTP_OPTIONS
HTTP_ANY
(Used for registering handlers, not typically returned bymethod()
)
You can compare the return value directly against these enumerated constants to check the request type, as shown in the Syntax and Usage section.
Example Codes
Displaying the Request Method and URI
This example sets up a basic ESP32 web server that responds to any request by displaying information about the request, including the URI and the HTTP Method used. Pay attention to the line inside the handleNotFound()
function where server.method()
is called to check if the request method is GET. Upload this code to your ESP32, connect it to your WiFi network, and access the ESP32’s IP address using a web browser (which typically makes a GET request). The response will indicate “GET” as the method. You could use tools like curl
or browser developer tools to send POST requests to observe the difference.
/*
* Author: Avant Maker
* Date: April 15, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* method() method to display information about HTTP request Method.
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
// Replace with your network credentials
const char* ssid = "your_SSID"; // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD"; // Replace with your Wi-Fi password
// Create a WebServer object on port 80
WebServer server(80);
// Function to handle requests when the specific path is not found
void handleNotFound() {
// Get the requested URI
String requestedUri = server.uri();
// Prepare the response message
String message = "AvantMaker Method() demo\n\n";
message += "URI Requested: ";
message += requestedUri;
message += "\nMethod: ";
// *** Use method() to determine the request method ***
message += (server.method() == HTTP_GET) ? "GET" : "POST"; // Simple check for GET vs other methods (like POST)
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
// Send the response to the client
server.send(404, "text/plain", message);
Serial.print("Handled request for URI: ");
Serial.print(requestedUri);
Serial.print(" with Method: ");
Serial.println((server.method() == HTTP_GET) ? "GET" : "POST"); // Also print method to Serial Monitor
}
void setup() {
Serial.begin(115200);
delay(100);
// Connect to Wi-Fi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Register the handler function for all requests not otherwise handled
// This handler uses both server.uri() and server.method()
server.onNotFound(handleNotFound);
// Start the server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Handle incoming client requests
server.handleClient();
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- Server Operation
- Client Hnadling
- Routing and Handlers
- Authentication
- Request Information
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!