ESP32 WebServer Library – sendContent()

Home / References / ESP32 Library / WebServer Library

Description

The sendContent() method in the ESP32 WebServer Library is used to send chunks of content as part of an HTTP response to a client. This method is particularly useful when you need to send data incrementally, such as when streaming large content or building a response dynamically. It is often used in conjunction with the send() method to handle the body of the HTTP response after headers have been set.


Syntax and Usage

The sendContent() method can be invoked in different ways depending on the type and size of the content you want to send. Below are the available usages:

  • Sending a String: Use this to send content stored in a String object directly.
  • Sending a C-string with Length: Use this to send a character array with a specified length, offering precise control over the data sent.

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 sendContent() method accepts different arguments depending on its usage. Here’s a breakdown:

  • content (String): A String object containing the content to send. Used in the sendContent(const String& content) variant.
  • content (const char*): A pointer to a null-terminated character array (C-string) to send. Used in the sendContent(const char* content, size_t contentLength) variant.
  • contentLength (size_t): The length of the content in bytes, required when sending a C-string to specify how much data to transmit. Used in the sendContent(const char* content, size_t contentLength) variant.

Return Value

The sendContent() method does not return a value (void). It sends the specified content directly to the client over the established connection. If chunked encoding is enabled (via a prior send() call with an unknown content length), it also handles the chunked transfer formatting automatically.


Example Codes

Example 1: Sending a String

This example shows how to send a simple HTML string incrementally using the String variant of sendContent().

This code demonstrates how to use the ESP32 WebServer Library’s sendContent() method to send a simple HTML string to a web browser. It sets up a basic web server that responds with a greeting message when a client accesses the root URL.

To use this code, replace the placeholders for your Wi-Fi SSID and password. Then, upload the code to your ESP32. Once connected to Wi-Fi, open a web browser and enter the ESP32’s IP address to see the “Hello from AvantMaker.com ESP32 Web Server!” message.

/*
 * Author: Avant Maker
 * Date: March 17, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * sendContent() method to send a simple HTML string.
 *
 * 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_SSID";          // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD";  // Replace with your Wi-Fi password3

WebServer server(80);

void handleRoot() {
  // Begin the response with headers
  server.setContentLength(CONTENT_LENGTH_UNKNOWN);
  server.send(200, "text/html", "");
  
  const char* message = "Hello from AvantMaker.com ESP32 Web Server!";
  server.sendContent(message);
  
  // End the response
  server.sendContent("");
}

void setup() {
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  Serial.println("Connected to WiFi");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

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

Example 2: Sending a C-string with Length

This code demonstrates how to use the ESP32 WebServer Library’s sendContent() method to send a fixed-length character array as a plain text response. It calculates the length of the message and uses setContentLength() to specify the content length before sending the message.

To use this code, replace the placeholders for your Wi-Fi SSID and password. Upload the code to your ESP32. Once connected to Wi-Fi, open a web browser and enter the ESP32’s IP address. You will see the plain text message “Hello from AvantMaker.com ESP32 Web Server!” displayed in the browser.

/*
 * Author: Avant Maker
 * Date: March 17, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * sendContent() method to send a fixed-length character array.
 *
 * 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() {
    // setup message
    const char* message = "Hello from AvantMaker.com ESP32 Web Server!";

    // Begin the response with headers
    server.setContentLength(strlen(message));
      
    server.send(200, "text/plain", ""); // Start response
    
    server.sendContent(message, strlen(message));

    // End the response
    server.sendContent("");
}

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected: " + WiFi.localIP().toString());
    server.on("/", handleRoot);
    server.begin();
}

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