Initialization¶
Creating the Client¶
ESP32HTTPClient is the entry point for all requests. Create one instance per server and reuse it — the library keeps the connection alive between calls automatically.
Constructor Signatures¶
// Standard port (80 for HTTP, 443 for HTTPS)
ESP32HTTPClient(const char* baseUrl);
// Custom port
ESP32HTTPClient(const char* baseUrl, int port);
Examples¶
Standard HTTPS (port 443)¶
Standard HTTP (port 80)¶
Custom Port¶
Useful for local servers, development environments, or IoT gateways:
Explicit Port on HTTPS¶
Placement¶
Declare the client at global scope so it persists across loop() iterations and the Keep-Alive connection is preserved:
// ✅ Correct — client lives for the lifetime of the program
ESP32HTTPClient client("https://api.example.com");
void setup() { ... }
void loop() {
// client is reused on every iteration
client.get("/data").getBody("value", &myVar);
}
// ❌ Avoid — a new client (and new TLS connection) is created on every loop
void loop() {
ESP32HTTPClient client("https://api.example.com"); // new handshake every time!
client.get("/data").getBody("value", &myVar);
}