ESP32: make HTTP requests
Make HTTP requests from your sketch with a clean, composable interface.
With this tutorial you'll learn how to make HTTP requests from your ESP32 sketch in the easiest and cleanest way possible. Say goodbye to lengthy, hard-to-remember, error prone code.
The library is meant to leverage your IDE's autocomplete feature. Just type httpx. and you'll see a list of suggestions for the available methods (get, post, put, delete) and options (Insecure, Cert, Header...).
GET
#include <esptoolkit.h>
#include <esptoolkit/httpx.h>
// skips SSL cert verification
// "Accept: text/plain" header
// "Referer" header
// connection and request timeout
auto response = httpx.get(
"https://icanhazdadjoke.com/",
httpx.Insecure(),
httpx.Accept("text/plain"),
httpx.Header("Referer", "arduino/esptoolkit"),
httpx.ConnectTimeout("2s"),
httpx.RequestTimeout("6s")
);
if (!response) {
Serial.print("Request failed with error: ");
Serial.println(response.reason());
}
else {
Serial.print("Request succeeded!");
Serial.println(response.text());
}POST
Use a POST request if the server expects some data.
// skips SSL cert verification
// "application/json" content type
// custom header
auto response = httpx.post(
"https://httpbin.org/post",
httpx.Insecure(),
httpx.Accept("application/json"),
httpx.ContentType("application/json"),
httpx.Header("X-Custom-Header", "esptoolkit"),
httpx.Body("[1, 2, 3]")
);
// assume request was ok
Serial.println(response.text());