Home / References / ESP32 Library / HTTPClient
Description
The getStream
method in the ESP32 HTTPClient Library retrieves a reference to the underlying network stream of an active HTTP connection. This method is particularly useful when you need direct access to the raw data stream from an HTTP response, enabling efficient handling of data without loading the entire response into memory. It’s ideal for processing large payloads or streaming data incrementally in your ESP32 projects.
Syntax and Usage
The getStream
method is invoked on an HTTPClient
object after initiating an HTTP request (e.g., via GET
or POST
). Below is the syntax and its usage:
NetworkClient& getStream(void)
This method has only one way of usage:
- Basic Usage: Call
getStream
after a successful HTTP request to access the raw data stream. This returns a reference to aNetworkClient
object, which you can use to read data directly as it arrives.
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 simply provides access to the stream associated with the current HTTP connection, assuming the connection is active.
Return Value
The getStream
method returns a reference to a NetworkClient
object representing the TCP stream of the HTTP connection. If the client is connected, this stream can be used to read incoming data. If the connection is not active, it returns an empty (default-constructed) NetworkClient
object, and a warning is logged to indicate the lack of connection.
Example Codes
Fetching and Streaming Data from an HTTP Response
This code demonstrates how to use the ESP32 HTTPClient
library’s getStream()
method to stream response data from an HTTP GET request. It sends a request to http://www.httpbin.org/get
and retrieves the response in chunks, which is useful for handling large responses without loading all data into memory at once.
To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi credentials. Upload the code to your ESP32 and open the Serial Monitor to observe the streaming process. The response data will be printed in chunks, showing how getStream()
efficiently handles incoming data.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's getStream to
* read and display the HTTP response data incrementally.
*
* 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);
Serial.println("\nConnecting to Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWi-Fi connected!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Serial.print("[HTTP] Beginning request...\n");
http.begin("http://www.httpbin.org/get"); // Send GET request to httpbin.org
Serial.print("[HTTP] Sending GET...\n");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] Response code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
// Get the stream
NetworkClient& stream = http.getStream();
int len = http.getSize(); // Get content length (-1 if not specified)
Serial.println("Streaming response data:");
uint8_t buffer[128] = {0}; // Buffer to read data
// Read data from the stream
while (stream.available() && (len > 0 || len == -1)) {
size_t size = stream.readBytes(buffer, sizeof(buffer));
Serial.write(buffer, size); // Print received data
if (len > 0) len -= size; // Update remaining length
delay(1); // Small delay to prevent overwhelming Serial
}
Serial.println("\nStreaming complete.");
}
} else {
Serial.printf("[HTTP] GET failed, error: %s\n", HTTPClient::errorToString(httpCode).c_str());
}
http.end(); // Close the connection
}
delay(10000); // Wait 10 seconds before the next request
}
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!