Home / References / ESP32 Library / WebServer Library
Description
The removeHandler()
method is used to dynamically remove a specific request handler that was previously registered with the WebServer instance using methods like on()
. Once a handler is removed, the server will no longer execute its associated function when a matching request (URI and HTTP method) is received.
This is particularly useful in scenarios where certain API endpoints or web pages need to be disabled or modified during runtime without restarting the ESP32.
Syntax and Usage
The removeHandler()
method is called on an instance of the WebServer
class. It requires a pointer to the specific RequestHandler
object that you wish to remove.
server.removeHandler(handlerToRemove);
- You must provide the exact
RequestHandler
object (as a pointer) that was returned when the handler was initially added (e.g., usingserver.on()
). Simply providing the URI path is not sufficient.
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument(s)
This method requires the following argument:
handlerToRemove
: A pointer (RequestHandler*
) to the specificRequestHandler
object that needs to be unregistered from the server. You typically save this pointer when you first register the handler function.
Return Value
This method does not return any value (void
).
Example Codes
The following example demonstrates how to add a request handler, store its reference, and then remove it later, perhaps triggered by another request.
Explanation:
- The code sets up the ESP32 to connect to your WiFi network.
- It initializes a
WebServer
on port 80. - It registers a handler for GET requests on the path
/data
usingserver.on()
. Crucially, it stores the returnedRequestHandler*
in thedataHandler
variable. - It registers another handler for GET requests on the path
/removeData
. When this path is accessed, the function associated with it callsserver.removeHandler(dataHandler)
to remove the/data
handler. It also sets thedataHandler
pointer tonullptr
as good practice. - The main loop continuously checks for incoming client connections using
server.handleClient()
. - To test:
- Upload the sketch to your ESP32 and open the Serial Monitor to get its IP address.
- Open a web browser and navigate to
http://<ESP32_IP>/data
. You should see “This is the data endpoint.”. - Navigate to
http://<ESP32_IP>/removeData
. You should see “Data handler removed.”. Check the Serial Monitor for confirmation. - Try navigating to
http://<ESP32_IP>/data
again. This time, you should get a “Not Found” error from the server (or a default handler response if one is set up), because the specific handler for/data
has been removed.
/*
* Author: Avant Maker
* Date: March 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* removeHandler() method to remove a handler from the server.
*
* 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
WebServer server(80);
// Variable to store the handler object we might want to remove
RequestHandler* dataHandler = nullptr;
// Handler function for the /data endpoint
void handleData() {
server.send(200, "text/plain", "This is the data endpoint.");
Serial.println("Handled /data request.");
}
// Handler function to remove the /data handler
void handleRemoveData() {
if (dataHandler != nullptr) {
server.removeHandler(dataHandler);
server.send(200, "text/plain", "Data handler removed.");
Serial.println("Handler for /data has been removed.");
dataHandler = nullptr; // Set pointer to null after removal
} else {
server.send(200, "text/plain", "Data handler was already removed or never added.");
Serial.println("Attempted to remove handler, but it was already null.");
}
}
// Handler for root path
void handleRoot() {
server.send(200, "text/html", "<h1>AvantMaker.comESP32 Web Server</h1><p>Visit /data to get data.</p><p>Visit /removeData to disable the /data endpoint.</p>");
Serial.println("Handled / request.");
}
// Handler for Not Found
void handleNotFound() {
server.send(404, "text/plain", "404: Not Found");
Serial.println("Handled 404 Not Found.");
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Connecting to WiFi...");
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 root handler
server.on("/", HTTP_GET, handleRoot);
// Register the /data handler and store its pointer
// Note the use of '&' before server.on() to get the address (pointer) of the returned RequestHandler object
dataHandler = &server.on("/data", HTTP_GET, handleData);
Serial.println("Registered handler for /data");
// Register the handler to remove the /data handler
server.on("/removeData", HTTP_GET, handleRemoveData);
Serial.println("Registered handler for /removeData");
// Register a Not Found handler
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started.");
}
void loop() {
server.handleClient();
// You could add other tasks here
// delay(10); // Small delay can be useful in some loops
}
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!