ESP32 HTTPClient Library – setAcceptEncoding

Home / References / ESP32 Library / HTTPClient

Description

The setAcceptEncoding method allows you to define the content encoding types (e.g., gzip, deflate) that the ESP32 HTTP client will accept from a server in response to an HTTP request. By setting this header, you can enable the server to compress the response data, reducing bandwidth usage and speeding up communication—ideal for resource-constrained IoT projects. This method enhances efficiency when retrieving data from web servers that support encoding.


Syntax and Usage

The setAcceptEncoding method is simple to use and requires a single argument to specify the encoding type. Below is the basic syntax:

http.setAcceptEncoding(encoding);

This method should be called after initializing the HTTPClient object and before sending the HTTP request. It sets the Accept-Encoding header in the request, informing the server of the client’s supported encoding types.

  • Using with a specific encoding: Provide a string like "gzip""deflate", or "identity" to indicate the preferred encoding. You can also use a comma-separated list (e.g., "gzip, deflate") to specify multiple supported encodings.

There is only one way to use this method, as it always requires an argument to define the encoding preference.

For more detailed information and examples of the syntax and usage 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)

  • encoding (const char*): A null-terminated string specifying the content encoding(s) the client accepts. Common values include "gzip" (compressed data using gzip), "deflate" (compressed data using zlib), or "identity" (no encoding). Multiple encodings can be listed, separated by commas (e.g., "gzip, deflate").

Return Value

The setAcceptEncoding method does not return a value. It configures the HTTPClient object by setting the Accept-Encoding header for the upcoming request.


Example Codes

Below is an example showcasing how to use the setAcceptEncoding method in an ESP32 project. This example connects to www.httpbin.org, a testing website that supports content encoding, to demonstrate the method in action.

Example: Requesting Gzip-Encoded Content

This code configures the ESP32 to request a gzip-encoded response from www.httpbin.org/get. It demonstrates how to use setAcceptEncoding to optimize data transfer by requesting compressed content.

In this example, the ESP32 requests data from www.httpbin.org/get with the Accept-Encoding: gzip header. If the server supports gzip, it sends a compressed response, which the HTTPClient library automatically decompresses before returning it via getString(). Check the content length in the Serial output to see the effect of compression (typically smaller than an uncompressed response). You can experiment with "deflate" or "gzip, deflate" to test other encodings supported by the server.

/*
 * Author: Avant Maker
 * Date: February 23, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's setAcceptEncoding
 * to optimize data transfer by requesting compressed content.
 *
 * License: MIT 
 * 
 * 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/references/esp32-arduino-core-index/
 *
 * 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 <HTTPClient.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

void setup() {
    Serial.begin(115200);

    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");

    // Initialize HTTPClient
    HTTPClient http;

    // Configure HTTP client
    http.begin("http://www.httpbin.org/get"); // Simple GET endpoint
    http.setAcceptEncoding("gzip"); // Request gzip-encoded response

    // Make the GET request
    int httpCode = http.GET();

    if (httpCode > 0) {
        String payload = http.getString(); // Automatically decompresses if gzip is used
        Serial.println("Response code: " + String(httpCode));
        Serial.println("Payload: " + payload);
        Serial.println("Content length: " + String(payload.length()));
    } else {
        Serial.println("Error on HTTP request: " + String(httpCode));
    }

    http.end(); // Free resources
}

void loop() {
    // Nothing to do here
}

Dive deeper into the ESP32 Arduino Core Library with more tutorials and resources on AvantMaker.com, and transform your creative ideas into reality!

error: Content is protected !!