Home / References / ESP32 Library / WebServer Library
Description
The hostHeader()
method of the ESP32 WebServer Library is used to retrieve the value of the Host
header from an incoming HTTP request. The Host
header is a standard HTTP request header field that specifies the domain name of the server (for virtual hosting) and optionally the TCP port number on which the server is listening. This information is sent by the client (such as a web browser) as part of the request headers to indicate which host they are trying to reach, especially when multiple websites are hosted on the same IP address.
Syntax and Usage
The hostHeader()
method is called on the WebServer object within a request handler function, after a client has connected and sent a request. It is used to access the value of the Host
header for the current request being processed.
Usage:
- Retrieve the Host header: Call the method directly on the WebServer instance (e.g.,
server
) to get the Host header value of the request currently being handled.
String host = server.hostHeader();
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. It operates on the current incoming HTTP request being processed by the server.
Return Value
The hostHeader()
method returns a String
object. This string contains the value of the Host
header as sent by the client in the HTTP request. If the Host
header is not present in the request (which is unusual for modern HTTP/1.1 requests but possible in some scenarios), the method will return an empty String
.
Example Codes
This example demonstrates how to set up a simple web server on the ESP32 that, when accessed, reads the Host
header from the incoming request and displays it on the webpage. You will need to replace "YOUR_SSID"
and "YOUR_PASSWORD"
with your actual WiFi network credentials.
/*
* Author: Avant Maker
* Date: April 30, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* hostHeader() method to retrieve the value of the Host header
* from an incoming HTTP request.
*
* 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 to the root URL "/"
void handleRoot() {
// Get the value of the Host header
String host = server.hostHeader();
String message = "<!DOCTYPE html><html><head><title>AvantMaker Host Header Example</title></head><body>";
message += "<h1>AvantMaker ESP32 WebServer hostHeader() Example</h1>";
message += "<p>Host header received: ";
// Check if the Host header was present and add its value to the message
if (host.length() > 0) {
message += host;
} else {
message += "Not found in request headers";
}
message += "</p>";
message += "<p>Access this page by typing the ESP32's IP address or hostname in your browser.</p>";
message += "</body></html>";
// Send the response to the client
server.send(200, "text/html", message);
}
void setup() {
Serial.begin(115200); // Start serial communication for debugging
// Connect to WiFi
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the ESP32's IP address
// Register the handler for the root URL
server.on("/", handleRoot);
// Start the web server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Listen for incoming clients and process their requests
server.handleClient();
}
Upload this sketch to your ESP32. Open the Serial Monitor to find the assigned IP address. Open a web browser on a device connected to the same network and navigate to that IP address. The page displayed will show the value of the Host header that your browser sent in the request.
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!