Home / References / ESP32 Library / WebServer Library
Description
The responseCode()
method of the ESP32 WebServer
library is used to retrieve the HTTP status code that has been set for the current response. This is helpful for debugging or for conditional logic based on the response status before it’s sent to the client.
When a client makes a request to the ESP32 web server, the server processes the request and prepares a response. Part of this response is an HTTP status code (e.g., 200 for OK, 404 for Not Found). While methods like send()
implicitly set a status code (usually 200 if not specified otherwise), the responseCode()
method allows you to check what this code is at any point after it has been determined by the server logic but before the headers are fully sent.
Syntax and Usage
This method is called on an instance of the WebServer
class.
int responseCode()
This is the standard way to use the method. It takes no arguments and returns the currently set HTTP response code.
#include <WebServer.h>
WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "Hello from ESP32!");
Serial.print("Response code sent: ");
Serial.println(server.responseCode()); // Will print 200 after send() determines it
}
void setup() {
Serial.begin(115200);
// WiFi connection code here...
// server.on("/", handleRoot);
// server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
Argument(s)
This method does not require any arguments.
Return Value
The responseCode()
method returns an int
representing the HTTP status code that has been set for the current response. Common values include:
200
: OK400
: Bad Request401
: Unauthorized403
: Forbidden404
: Not Found500
: Internal Server Error
If no response has been explicitly set or sent yet, the value might be less predictable or default to a library-specific initial value (often 0 or a non-standard code if accessed too early), though typically you would call this after a response is being prepared (e.g., after calling server.send()
or server.sendHeader()
implicitly sets it).
For a complete list of all possible values defined in the ESP32 HTTPClient Library, please visit the following webpage
ESP32 HTTPClient Library – HTTP Status Codes and Error Codes
Example Codes
Below are example codes demonstrating the usage of the responseCode()
method.
Example 1: Checking Response Code After Sending a Response
This example demonstrates how to start a simple web server, send a response to a client request, and then print the HTTP status code of that response to the Serial Monitor. This is useful to confirm what status code was actually used by the send()
method.
/*
* Author: Avant Maker
* Date: May 30, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* responseCode() method to get HTTP status code and print to the Serial Monitor.
* This is useful to confirm what status code was actually used by the send() 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";
const char* password = "YOUR_PASSWORD";
WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "Hello from AvantMaker!");
Serial.print("Handler: Sent response with code: ");
Serial.println(server.responseCode()); // Expected to be 200
}
void handleNotFound() {
server.send(404, "text/plain", "404: Not Found");
Serial.print("Handler: Sent response with code: ");
Serial.println(server.responseCode()); // Expected to be 404
}
void setup() {
Serial.begin(115200);
delay(1000);
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.println("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started. Open http://your-esp32-ip/ in your browser.");
Serial.println("Try also accessing http://your-esp32-ip/nonexistentpage");
}
void loop() {
server.handleClient();
}
Explanation:
- Replace
"YOUR_SSID"
and"YOUR_PASSWORD"
with your WiFi credentials. - Upload the sketch to your ESP32.
- Open the Serial Monitor to see the ESP32’s IP address.
- Navigate to
http://<ESP32_IP_ADDRESS>/
in your web browser. The ESP32 will send a “Hello from ESP32!” message with a 200 OK status. The Serial Monitor will print “Handler: Sent response with code: 200”. - Navigate to
http://<ESP32_IP_ADDRESS>/nonexistentpage
. The ESP32 will send a “404: Not Found” message. The Serial Monitor will print “Handler: Sent response with code: 404”.
This example shows that responseCode()
correctly reflects the status code set by the server.send()
method for different handlers.
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!