Home / References / ESP32 Library / WebServer Library
Description
The collectAllHeaders()
method is used to configure the ESP32 WebServer to automatically collect all HTTP request headers from incoming client requests. Unlike the collectHeaders()
method which requires you to specify individual header names, this method sets the server to capture every header sent by the client. This is particularly useful for debugging purposes, comprehensive request logging, security analysis, or when you need to access any header without knowing in advance which headers clients might send. While convenient, this method consumes more memory than selective header collection, so it should be used judiciously in memory-constrained applications. The method internally sets a flag that instructs the server to parse and store all incoming headers, making them accessible through the header()
, headerName()
, and hasHeader()
methods.
Syntax and Usage
The collectAllHeaders()
method has a simple usage pattern:
- Collect all headers:
server.collectAllHeaders()
– Configures the server to collect all incoming HTTP headers automatically. This should be called during setup, before starting the server.
Arguments
The collectAllHeaders()
method does not require any arguments. It is a parameterless method that enables automatic collection of all headers.
Return Value
The collectAllHeaders()
method does not return any value (void return type). The method configures the WebServer internally to collect all headers, which can then be accessed using the header()
, headerName()
, and hasHeader()
methods during request processing.
Example Codes
Example 1: Basic All Headers Collection
This example demonstrates the fundamental usage of collectAllHeaders() to automatically capture and display all HTTP headers sent by clients, providing complete visibility into request headers.
How to use this example: Upload this code to your ESP32 and replace the WiFi credentials. After connecting, visit the ESP32’s IP address in your browser to see all headers your browser sends. Test with different clients: curl commands like curl -H "Custom-Header: test-value" -H "X-API-Key: 12345" http://ESP32_IP
, different browsers, or mobile devices to see how header sets vary between clients.
/*
* Author: Avant Maker
* Date: June 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates the fundamental usage of collectAllHeaders() to automatically capture and display all HTTP headers sent by clients, providing complete visibility into request headers.
*
* How to use this example:
* Upload this code to your ESP32 and replace the WiFi credentials.
* After connecting, visit the ESP32's IP address in your browser
* to see all headers your browser sends. Test with different clients:
* curl commands like curl -H "Custom-Header: test-value" -H "X-API-Key: 12345" http://ESP32_IP,
* different browsers, or mobile devices to see how header sets vary between clients.
*
* 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 nnovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
WebServer server(80);
void handleRoot() {
String message = "AvantMaker ESP32 Complete Header Collection\n";
message += "=================================\n\n";
int totalHeaders = server.headers();
message += "Total headers received: " + String(totalHeaders) + "\n\n";
if (totalHeaders > 0) {
message += "All Request Headers:\n";
message += "--------------------\n";
// Display all collected headers with their index, name, and value
for (int i = 0; i < totalHeaders; i++) {
String headerName = server.headerName(i);
String headerValue = server.header(i);
message += String(i + 1) + ". " + headerName + ": " + headerValue + "\n";
}
message += "\nHeader Analysis:\n";
message += "----------------\n";
message += "Shortest header name: ";
message += findShortestHeaderName() + "\n";
message += "Longest header name: ";
message += findLongestHeaderName() + "\n";
message += "Total header data size: " + String(calculateTotalHeaderSize()) + " bytes\n";
} else {
message += "No headers were collected!\n";
message += "This shouldn't happen if collectAllHeaders() was called properly.\n";
}
server.send(200, "text/plain", message);
}
String findShortestHeaderName() {
String shortest = "";
int minLength = 1000;
for (int i = 0; i < server.headers(); i++) {
String name = server.headerName(i);
if (name.length() < minLength) {
minLength = name.length();
shortest = name;
}
}
return shortest;
}
String findLongestHeaderName() {
String longest = "";
int maxLength = 0;
for (int i = 0; i < server.headers(); i++) {
String name = server.headerName(i);
if (name.length() > maxLength) {
maxLength = name.length();
longest = name;
}
}
return longest;
}
int calculateTotalHeaderSize() {
int totalSize = 0;
for (int i = 0; i < server.headers(); i++) {
totalSize += server.headerName(i).length();
totalSize += server.header(i).length();
totalSize += 2; // for ": " separator
}
return totalSize;
}
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Configure server to collect ALL headers automatically
server.collectAllHeaders();
// Set up route handler
server.on("/", handleRoot);
// Start server
server.begin();
Serial.println("HTTP server started");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Server configured to collect ALL headers");
}
void loop() {
server.handleClient();
}
Example 2: Header Categorization and Analysis System
This example shows how to use collectAllHeaders() to build a comprehensive header analysis system that categorizes headers by type and provides detailed insights into client behavior.
How to use this example: After uploading and connecting to WiFi, access the ESP32’s IP address to see the main interface, then try different endpoints. Test with various header combinations: curl -H "Authorization: Bearer token123" -H "Accept: application/json" -H "X-Custom-API: v2.1" http://ESP32_IP/analyze
to see how headers are categorized. Visit /debug for comprehensive analysis and /stats for statistical overview.
/*
* Author: Avant Maker
* Date: June 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example shows how to use collectAllHeaders() to build a
* comprehensive header analysis system that categorizes headers
* by type and provides detailed insights into client behavior.
*
* How to use this example:
* After uploading and connecting to WiFi, access the ESP32's IP
* address to see the main interface, then try different endpoints.
* Test with various header combinations:
* curl -H "Authorization: Bearer token123" -H "Accept: application/json" -H "X-Custom-API: v2.1" http://ESP32_IP/analyze
* to see how headers are categorized. Visit /debug for comprehensive
* analysis and /stats for statistical overview.
*
* 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 nnovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
WebServer server(80);
struct HeaderStats {
int standardHeaders = 0;
int authHeaders = 0;
int contentHeaders = 0;
int customHeaders = 0;
int securityHeaders = 0;
int cacheHeaders = 0;
int total = 0;
};
HeaderStats analyzeHeaders() {
HeaderStats stats;
stats.total = server.headers();
for (int i = 0; i < stats.total; i++) {
String headerName = server.headerName(i);
String lowerName = headerName;
lowerName.toLowerCase();
// Categorize headers based on their names and purposes
if (lowerName.indexOf("auth") != -1 || lowerName.equals("authorization") ||
lowerName.indexOf("token") != -1) {
stats.authHeaders++;
} else if (lowerName.indexOf("content") != -1 || lowerName.indexOf("accept") != -1 ||
lowerName.indexOf("encoding") != -1) {
stats.contentHeaders++;
} else if (lowerName.indexOf("x-") == 0 || lowerName.indexOf("custom") != -1) {
stats.customHeaders++;
} else if (lowerName.indexOf("sec-") == 0 || lowerName.indexOf("origin") != -1 ||
lowerName.indexOf("referer") != -1) {
stats.securityHeaders++;
} else if (lowerName.indexOf("cache") != -1 || lowerName.indexOf("etag") != -1 ||
lowerName.indexOf("modified") != -1) {
stats.cacheHeaders++;
} else {
stats.standardHeaders++;
}
}
return stats;
}
void handleAnalyze() {
String response = "Advanced Header Analysis Report\n";
response += "==============================\n\n";
HeaderStats stats = analyzeHeaders();
response += "Header Categories:\n";
response += "-----------------\n";
response += "🔐 Authentication: " + String(stats.authHeaders) + "\n";
response += "📄 Content/Accept: " + String(stats.contentHeaders) + "\n";
response += "🛡️ Security: " + String(stats.securityHeaders) + "\n";
response += "💾 Cache: " + String(stats.cacheHeaders) + "\n";
response += "⚙️ Custom: " + String(stats.customHeaders) + "\n";
response += "📊 Standard: " + String(stats.standardHeaders) + "\n";
response += "📈 Total: " + String(stats.total) + "\n\n";
response += "Detailed Header List:\n";
response += "--------------------\n";
for (int i = 0; i < stats.total; i++) {
String name = server.headerName(i);
String value = server.header(i);
String category = "";
// Add category prefix
String lowerName = name;
lowerName.toLowerCase();
if (lowerName.indexOf("auth") != -1 || lowerName.equals("authorization")) {
category = "[🔐AUTH] ";
} else if (lowerName.indexOf("content") != -1 || lowerName.indexOf("accept") != -1) {
category = "[📄CONTENT] ";
} else if (lowerName.indexOf("x-") == 0) {
category = "[⚙️CUSTOM] ";
} else if (lowerName.indexOf("sec-") == 0 || lowerName.indexOf("origin") != -1) {
category = "[🛡️SECURITY] ";
} else if (lowerName.indexOf("cache") != -1 || lowerName.indexOf("etag") != -1) {
category = "[💾CACHE] ";
} else {
category = "[📊STANDARD] ";
}
response += category + name + ": " + value + "\n";
}
// Calculate percentages
if (stats.total > 0) {
response += "\nHeader Distribution:\n";
response += "-------------------\n";
response += "Authentication: " + String((float)stats.authHeaders / stats.total * 100, 1) + "%\n";
response += "Content-related: " + String((float)stats.contentHeaders / stats.total * 100, 1) + "%\n";
response += "Security: " + String((float)stats.securityHeaders / stats.total * 100, 1) + "%\n";
response += "Custom: " + String((float)stats.customHeaders / stats.total * 100, 1) + "%\n";
}
server.send(200, "text/plain", response);
}
void handleDebug() {
String response = "Debug Information\n";
response += "=================\n\n";
response += "Client Information:\n";
response += "-------------------\n";
response += "IP Address: " + server.client().remoteIP().toString() + "\n";
response += "Request Method: " + String((server.method() == HTTP_GET) ? "GET" :
(server.method() == HTTP_POST) ? "POST" : "OTHER") + "\n";
response += "Request URI: " + server.uri() + "\n";
response += "HTTP Version: " + server.version() + "\n\n";
response += "Raw Header Dump:\n";
response += "================\n";
int count = server.headers();
for (int i = 0; i < count; i++) {
response += "Header[" + String(i) + "]: \"";
response += server.headerName(i) + "\" = \"";
response += server.header(i) + "\"\n";
}
if (count == 0) {
response += "WARNING: No headers collected!\n";
response += "Ensure collectAllHeaders() was called in setup().\n";
}
server.send(200, "text/plain", response);
}
void handleStats() {
HeaderStats stats = analyzeHeaders();
String html = "<html><body>";
html += "<h1>AvantMaker ESP32 Header Statistics</h1>";
html += "<h2>Live Header Analysis</h2>";
html += "<table border='1' style='border-collapse:collapse;'>";
html += "<tr><th>Category</th><th>Count</th><th>Percentage</th></tr>";
if (stats.total > 0) {
html += "<tr><td>🔐 Authentication</td><td>" + String(stats.authHeaders) + "</td><td>" + String((float)stats.authHeaders / stats.total * 100, 1) + "%</td></tr>";
html += "<tr><td>📄 Content/Accept</td><td>" + String(stats.contentHeaders) + "</td><td>" + String((float)stats.contentHeaders / stats.total * 100, 1) + "%</td></tr>";
html += "<tr><td>🛡️ Security</td><td>" + String(stats.securityHeaders) + "</td><td>" + String((float)stats.securityHeaders / stats.total * 100, 1) + "%</td></tr>";
html += "<tr><td>💾 Cache</td><td>" + String(stats.cacheHeaders) + "</td><td>" + String((float)stats.cacheHeaders / stats.total * 100, 1) + "%</td></tr>";
html += "<tr><td>⚙️ Custom</td><td>" + String(stats.customHeaders) + "</td><td>" + String((float)stats.customHeaders / stats.total * 100, 1) + "%</td></tr>";
html += "<tr><td>📊 Standard</td><td>" + String(stats.standardHeaders) + "</td><td>" + String((float)stats.standardHeaders / stats.total * 100, 1) + "%</td></tr>";
html += "<tr><td><strong>Total</strong></td><td><strong>" + String(stats.total) + "</strong></td><td><strong>100%</strong></td></tr>";
} else {
html += "<tr><td colspan='3'>No headers collected</td></tr>";
}
html += "</table>";
html += "<p><a href='/analyze'>Detailed Analysis</a> | <a href='/debug'>Debug Info</a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleRoot() {
String html = "<html><body>";
html += "<h1>ESP32 Complete Header Collection System</h1>";
html += "<p>This system automatically collects and analyzes ALL HTTP headers.</p>";
html += "<h2>Available Endpoints:</h2>";
html += "<ul>";
html += "<li><a href='/stats'>/stats</a> - Visual header statistics</li>";
html += "<li><a href='/analyze'>/analyze</a> - Detailed header analysis</li>";
html += "<li><a href='/debug'>/debug</a> - Raw header dump</li>";
html += "</ul>";
html += "<p><em>Currently collecting " + String(server.headers()) + " headers from your request.</em></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Enable automatic collection of ALL headers
server.collectAllHeaders();
// Set up route handlers
server.on("/", handleRoot);
server.on("/stats", handleStats);
server.on("/analyze", handleAnalyze);
server.on("/debug", handleDebug);
// Start server
server.begin();
Serial.println("HTTP server started");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Collecting ALL headers automatically");
}
void loop() {
server.handleClient();
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 AsyncUDP Librarry
- ESP32 WebServer Library
- Server Operation
- Client Hnadling
- Routing and Handlers
- Authentication
- Request Information
- Request Header Management
- Response Information
- Server Configuration
- 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!