ESP32 WebServer Library – sendHeader()

Home / References / ESP32 Library / WebServer Library

Description

The sendHeader() method in the ESP32 WebServer Library is used to add custom HTTP headers to the response sent by the ESP32 web server. HTTP headers provide additional metadata about the response, such as content type, caching instructions, or custom information required by the client. This method is essential for fine-tuning the behavior of your web server and ensuring compatibility with various clients, making it a powerful tool for creating dynamic and responsive web applications on the ESP32.


Syntax and Usage

The sendHeader() method is called on a WebServer object and allows you to specify a header name, its value, and an optional flag to control its position in the response. Below is the syntax and usage:

void sendHeader(const String& name, const String& value, bool first = false);

This method can be used in the following way:

  • Adding a header to the response: Use this to append a custom header to the list of headers that will be sent with the HTTP response.
  • Inserting a header as the first in the response: By setting the optional first argument to true, the header is placed at the beginning of the header list instead of being appended.

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)

The sendHeader() method accepts the following arguments:

  • name (const String&): The name of the HTTP header (e.g., “Content-Type”, “Cache-Control”). This is a required argument and defines the header field.
  • value (const String&): The value associated with the header name (e.g., “text/html”, “no-cache”). This is a required argument and specifies the content of the header.
  • first (bool, optional): A boolean flag that determines the position of the header in the response. If set to true, the header is inserted as the first one; if false (default), it is appended to the end of the header list.

Return Value

The sendHeader() method does not return any value (i.e., its return type is void). It modifies the internal state of the WebServer object by adding the specified header to the response, which is then sent to the client when the response is finalized.


Example Codes

Below are example codes demonstrating the two ways to use the sendHeader() method as outlined in Section 2. Each example includes an explanation to help you understand its application.

Example 1: Adding a Header to the Response

This example shows how to use sendHeader() to add a “Cache-Control” header to prevent the client from caching the response. The header is appended to the list by default.

Explanation: In this code, the sendHeader() method adds a “Cache-Control: no-cache” header to the response. When a client accesses the root URL (“/”), it receives an HTML page with this header, ensuring the browser always requests the latest content from the ESP32.

To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. Then, upload the code to your ESP32. Once connected to Wi-Fi, open a web browser and navigate to the ESP32’s IP address, which is printed to the Serial Monitor. You will see “Hello from AvantMaker ESP32 Web Server!” displayed, and the browser will not cache the page.

/*
 * Author: Avant Maker
 * Date: March 17, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * sendHeader() method to add a "Cache-Control" header to prevent
 * the client from caching the response. The header is appended
 * to the list by default.
 *
 * 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>

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);

void handleRoot() {
    server.sendHeader("Cache-Control", "no-cache"); // Add header to disable caching
    server.send(200, "text/html", "<h1>Hello from AvantMaker ESP32 Web Server!</h1>");
}

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");
    Serial.println(WiFi.localIP());

    server.on("/", handleRoot);
    server.begin();
}

void loop() {
    server.handleClient();
}

Example 2: Inserting a Header as the First in the Response

This example demonstrates using sendHeader() with the first argument set to true to insert a “Server” header at the beginning of the header list, followed by another header.

Explanation: Here, sendHeader("Server", "ESP32-WebServer", true) ensures the “Server” header is the first in the response, followed by the “Content-Type” header. The client receives a plain text message with these headers in the specified order, which can be useful for debugging or meeting specific protocol requirements.

To run this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi network details. Upload the code to your ESP32. After connecting to Wi-Fi, open a web browser and enter the ESP32’s IP address, which is displayed in the Serial Monitor. You will see “Welcome to AvantMaker’s ESP32 Server!” displayed as plain text. The “Server” header will be the first header in the HTTP response, and “Content-Type” will follow.

/*
 * Author: Avant Maker
 * Date: March 17, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * sendHeader() method with the first argument set to true to
 * insert a "Server" header at the beginning of the header list,
 * followed by another header.
 *
 * 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>

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);

void handleRoot() {
    server.sendHeader("Server", "ESP32-WebServer", true); // Insert as first header
    server.sendHeader("Content-Type", "text/plain");      // Appended after
    server.send(200, "text/plain", "Welcome to AvantMaker's ESP32 Server!");
}

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");
    Serial.println(WiFi.localIP());

    server.on("/", handleRoot);
    server.begin();
}

void loop() {
    server.handleClient();
}
error: Content is protected !!