Home / References / ESP32 Library / HTTPClient
Description
The getSize
method in the ESP32 HTTPClient Library retrieves the size of the message body (payload) from an HTTP response. This is particularly useful when you need to know the content length provided by the server, allowing you to allocate resources or process data efficiently in your projects. It relies on the Content-Length
header sent by the server, making it a key tool for handling HTTP responses dynamically.
Syntax and Usage
The getSize
method is straightforward to use within your HTTPClient workflow. Below is the syntax, followed by its typical application:
int size = http.getSize();
This method has only one way of usage, as it does not accept any arguments. It is called after initiating an HTTP request (e.g., GET
, POST
) and processing the server’s response headers. Here’s how it fits into your code:
- Standard Usage: Invoke
getSize
after a successful HTTP request to determine the payload size based on theContent-Length
header. This is ideal for planning how to read or store the response data.
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 solely on the HTTP response data already received by the HTTPClient instance, making it simple and intuitive to integrate into your sketches.
Return Value
The getSize
method returns an integer representing the size of the response payload:
- Positive Integer: If the server provides a
Content-Length
header, this value indicates the exact size of the payload in bytes. - -1: If the server does not specify a
Content-Length
header (e.g., when using chunked encoding or no content is present), the method returns -1.
This return value helps you decide how to proceed with reading the response, whether it’s a fixed-size payload or a stream of unknown length.
Example Codes
Below is an example demonstrating the use of the getSize
method in a practical scenario. This aligns with the standard usage described above and connects to www.httpbin.org
for a real-world test.
Example: Retrieving Payload Size from an HTTP GET Request
This example connects an ESP32 to Wi-Fi, performs a GET request to www.httpbin.org/get
, and uses getSize
to check the response payload size. It’s a great starting point for understanding how to handle HTTP responses in your projects.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's getSize to check
* the HTTP response payload size.
*
* 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-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://www.httpbin.org/get"); // Send GET request to httpbin.org
int httpCode = http.GET(); // Execute the request
if (httpCode > 0) { // Check for a successful response
int payloadSize = http.getSize(); // Get the size of the response payload
Serial.print("HTTP Response Code: ");
Serial.println(httpCode);
Serial.print("Payload Size: ");
Serial.println(payloadSize);
if (payloadSize > 0) {
String payload = http.getString(); // Read the payload if size is known
Serial.println("Response Payload:");
Serial.println(payload);
} else {
Serial.println("No Content-Length provided by server.");
}
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpCode);
}
http.end(); // Free resources
}
delay(10000); // Wait 10 seconds before the next request
}
Explanation: After establishing a Wi-Fi connection, the ESP32 sends a GET request to www.httpbin.org/get
, a service that returns a simple JSON response. The getSize
method is called to retrieve the payload size. If the size is positive, the payload is read and printed; otherwise, a message indicates that no size was provided (e.g., due to chunked encoding). This example showcases how getSize
helps you manage response data effectively.
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 HTTPClient Library
- Connection
- Request Methods
- Request Config
- Response
- Cookie
- 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!