Home / References / ESP32 Library / WebServer Library
Description
The uri()
method is a member function of the ESP32 WebServer
class. When a client makes an HTTP request to the ESP32 server, this method is used within a request handler function to retrieve the Uniform Resource Identifier (URI) path specified in that request. The URI is the part of the URL that identifies the specific resource being requested (e.g., “/”, “/data”, “/index.html”).
Syntax and Usage
The uri()
method is called on an instance of the WebServer
object, typically from within a function designated to handle incoming client requests.
server.uri()
This is the standard way to use the method. It is called without any arguments and returns the requested URI string.// Assuming 'server' is your WebServer object instance String requestedUri = server.uri();
This syntax is used inside handler functions registered using methods likeserver.on()
orserver.onNotFound()
to determine which resource the client is asking for.
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 does not require any arguments.
Return Value
The uri()
method returns an Arduino String
object. This string contains the path part of the URI from the client’s HTTP request. For example:
- If the client requests
http://<ESP32_IP>/
,uri()
returns"/"
. - If the client requests
http://<ESP32_IP>/sensor/temp
,uri()
returns"/sensor/temp"
. - If the client requests
http://<ESP32_IP>/style.css
,uri()
returns"/style.css"
.
Note that the returned string does not include the protocol, domain name/IP address, port, or query parameters (the part after ‘?’).
Example Codes
Displaying the Requested URI
This example demonstrates how to set up a basic ESP32 web server that responds to any request by displaying the URI that the client requested. Upload this code to your ESP32, connect it to your WiFi network, and then navigate to different paths on the ESP32’s IP address (e.g., http://<YourESP32IP>/
, http://<YourESP32IP>/hello
, http://<YourESP32IP>/some/path
) using a web browser. The browser will display the requested URI.
/*
* Author: Avant Maker
* Date: April 15, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* uri() method to display the URI that the client requested
*
* 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 using the uri() method
String requestedUri = server.uri();
// Prepare the response message
String message = "AvantMaker uri method demo\n\n";
message += "URI Requested: ";
message += requestedUri; // Append the actual URI
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "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.println(requestedUri);
}
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 is where we will use server.uri()
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!