A lightweight asynchronous and synchronous HTTP client for ESP32 with automatic redirects, retries, HTTPS support, chunked transfers, and detailed debugging.
Author: Davide
License: MIT
Version: 2.0 (AsyncClient Evolution)
- #-what-is-it
- #-key-features
- #-why-asynchttpclientlight
- #-installation
- #-quick-start
- #-usage-modes
- #-main-api
- #-callback-events
- #-https
- #-included-examples
- #-logging
- #-important-notes
- #-troubleshooting
- #-data-structures
- #-quick-reference
- #-suggested-tests
- #-contributing
- #-license
AsyncHTTPClientLight is a lightweight HTTP client library for ESP32 that provides both:
- Asynchronous (non-blocking) requests
- Synchronous (blocking) requests
Designed as a stable alternative to Arduino's HTTPClient, it includes:
- Automatic redirect handling
- Smart retry mechanism
- HTTPS support
- Chunked transfer support
- Advanced logging
- Configurable response buffers
- PSRAM support for large payloads
Ideal for:
- IoT telemetry
- Cloud data logging
- REST APIs
- Google Sheets integrations
- Adafruit IO
- ESP32 FreeRTOS applications
| Feature | Description |
|---|---|
| β Async Requests | Non-blocking state-machine architecture |
| β Sync Requests | Traditional blocking API |
| β Mixed Mode | Use sync and async together |
| β Automatic HTTPS | Enables WiFiClientSecure automatically |
| β Redirects | Supports 301, 302, 303, 307, 308 |
| β Smart Retries | Configurable retry mechanism |
| β Chunked Transfer | Full support |
| β Custom Headers | Authorization, API keys, custom tokens |
| β Unified Callbacks | Single event handler |
| β Logging | Serial, SD, SPIFFS, LittleFS |
| β PSRAM Support | Large payload downloads |
| β FreeRTOS Friendly | ESP32 multitasking ready |
| Feature | HTTPClient | AsyncHTTPClientLight |
|---|---|---|
| Non-blocking requests | β | β |
| Unified callback system | β | β |
| Automatic redirects | β | |
| Retry handling | β | β |
| Chunked transfer support | β | |
| Detailed logging | β | β |
| PSRAM payload buffers | β | β |
| ESP32 multitasking support | β |
- Clone or download this repository.
git clone https://github.com/copida/AsyncHTTPClientLight.git- Copy the folder into your Arduino libraries directory:
~/Arduino/libraries/AsyncHTTPClientLight
- Restart Arduino IDE.
Coming soon.
- Arduino.h
- WiFiClient.h
- WiFiClientSecure.h
- STL
vector - STL
functional
Optional:
- SD.h
- SPIFFS.h
- LittleFS.h
#include <WiFi.h>
#include "AsyncHTTPClientLight.h"
AsyncHTTPClientLight http;
void handleEvent(HTTPEventType type,
const HTTPResponse* res) {
if (type == HTTPEventType::Response) {
Serial.printf("HTTP %d\n", res->statusCode);
Serial.println(http.getResponsePayload());
}
}
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
http.setDebug(true);
http.onEvent(handleEvent);
http.addTitle("My Request");
http.beginRequest(
"https://api.example.com/data",
"GET"
);
}
void loop() {
http.poll(); // REQUIRED
delay(100);
}void setup() {
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
int httpCode = http.runSync(
"https://api.example.com/data",
"GET"
);
Serial.printf(
"HTTP %d\n",
httpCode
);
Serial.println(
http.getResponsePayload()
);
}
void loop() {}http.beginRequest(
url,
"POST",
jsonPayload
);
// loop()
http.poll();
if (http.isFinished()) {
Serial.println(
http.getResponsePayload()
);
}Advantages
- Main loop remains responsive
- Sensors continue working
- Displays update correctly
- Event-based notifications
Disadvantages
- Slightly more complex
- One request at a time
int code =
http.runSync(url, "GET");
Serial.println(
http.getResponsePayload()
);Advantages
- Simple API
- Immediate response
Disadvantages
- Blocks execution
- No multitasking while waiting
void loop() {
http.poll();
if (someEvent) {
int code =
http.runSync(
url,
"GET"
);
// Completes any async
// request currently running
}
}| Function | Description |
|---|---|
beginRequest() |
Start async request |
runSync() |
Perform synchronous request |
poll() |
Drive async state machine |
isFinished() |
Request completed? |
getLastHTTPcode() |
Last HTTP code |
getResponsePayload() |
Response buffer |
setResponsePayload() |
Custom response buffer |
addHeader() |
Add HTTP header |
addTitle() |
Request logging label |
setTimeout() |
Request timeout |
setMaxRetries() |
Retry count |
setmaxRedirects() |
Maximum redirects |
setDebug() |
Enable Serial logging |
setLogToFile() |
File logging |
onEvent() |
Event callback |
void handleEvent(
HTTPEventType type,
const HTTPResponse* res
) {
switch (type) {
case HTTPEventType::Response:
Serial.printf(
"β
HTTP %d\n",
res->statusCode
);
Serial.println(
http.getResponsePayload()
);
break;
case HTTPEventType::Timeout:
Serial.printf(
"β±οΈ Timeout: %s\n",
res->msg_error
);
break;
case HTTPEventType::Error:
Serial.printf(
"β Error: %s\n",
res->msg_error
);
break;
case HTTPEventType::Overload:
Serial.println(
"β οΈ Request already running"
);
break;
case HTTPEventType::Chunk:
Serial.printf(
"π¦ Chunk: %d bytes\n",
res->contentLength
);
break;
default:
break;
}
}
β οΈ getResponsePayload()is valid only inside the callback.
HTTPS is enabled automatically whenever the URL starts with:
https://
Example:
http.beginRequest(
"https://api.example.com/data",
"GET"
);By default the library uses:
setInsecure();This means:
β
Traffic is encrypted
β Server identity is NOT verified
Suitable for:
- Telemetry
- Sensors
- Public APIs
- Logging
Not recommended for:
- Passwords
- Authentication tokens
- Sensitive user data
For production-grade security, certificate validation with setCACert() should be implemented.
Simple blocking GET and POST requests.
Features:
- Periodic JSON upload
- Google Apps Script redirects
- Unified callback handling
- WiFi auto-reconnect
Features:
- 50KB PSRAM buffer
- ArduinoJson v7 support
- API authentication headers
- Historical data download
http.setDebug(true);Example output:
[REQ 1] === My Request ===
[REQ 1] Protocol: HTTPS
[REQ 1] Host: api.example.com
[REQ 1] Port: 443
[REQ 1] Attempt 1
[REQ 1] Connection successful
[REQ 1] Sending...
[REQ 1] Status code: 200
[REQ 1] Content-Length: 512
Inside:
src/AsyncHTTPClientLight.cppConfigure:
#define ASYNC_HTTP_DEBUG 1
#if ASYNC_HTTP_DEBUG
#define ASYNC_HTTP_LOG_SD
#define MAXSIZEFILE_LOG 512000
#endifEnable:
http.setLogToFile(true);Generated files:
/http_log.txt
/old_Log.txt
β Correct:
void handleEvent(
HTTPEventType type,
const HTTPResponse* res
) {
if (type == HTTPEventType::Response) {
Serial.println(
http.getResponsePayload()
);
}
}β Wrong:
Serial.println(
http.getResponsePayload()
);The buffer may already have been released.
void loop() {
http.poll();
// Your application code
}if (http.isFinished()) {
http.beginRequest(
url,
"GET"
);
} else {
Serial.println(
"Previous request still running"
);
}char* bigBuffer =
(char*)ps_malloc(50000);
http.setResponsePayload(
bigBuffer,
50000
);
http.beginRequest(
url,
"GET"
);
free(bigBuffer);http.setDebug(true);Review log messages in Serial Monitor.
http.setTimeout(15000);http.setmaxRedirects(5);http.setResponsePayload(
largeBuffer,
bufferSize
);Verify:
http.poll();is called every loop iteration.
if (WiFi.status() != WL_CONNECTED) {
Serial.println(
"WiFi lost, reconnecting..."
);
WiFi.reconnect();
return;
}struct HTTPResponse {
int statusCode;
uint32_t restime;
char inprogressTitle[64];
char contentType[45];
int contentLength;
bool isStream;
bool isChunked;
int expectedLength;
char* ptr_workbuffer;
char msg_error[50];
};enum class HTTPEventType {
Response,
Error,
Timeout,
Overload,
Chunk,
Receiving,
Line
};| Use Case | API |
|---|---|
| Setup / Configuration | runSync() |
| Periodic Telemetry | beginRequest() + poll() |
| Large Downloads | setResponsePayload() |
| Bearer Tokens | addHeader() |
| Google Sheets | setmaxRedirects() |
| Adafruit IO | setmaxRedirects() |
| Troubleshooting | setDebug() |
https://httpbin.org/delay/5
https://httpbin.org/status/404
https://httpbin.org/json
https://httpbin.org/post
Contributions are welcome.
Feel free to:
- Open Issues
- Submit Pull Requests
- Report bugs
- Suggest new features
If this library helped you:
- β Star the repository
- π Report bugs
- π‘ Suggest improvements
- π’ Share it with other ESP32 developers
Released under the MIT License.
See the LICENSE file for details.
Maintainer: Davide (copida.soft@gmail.com)
Repository:
https://github.com/copida/AsyncHTTPClientLight