Arduino library · Network

ArduinoHttpClient: HTTP requests on any network Client

ArduinoHttpClient is Arduino’s own HTTP library. Its selling point is that it sits on top of any Client object: a WiFiClient on an ESP32 or Uno R4 WiFi, a WiFiNINA client on a Nano 33 IoT, an EthernetClient, or a GSM modem’s client. The same request code then works across boards, which is not true of the ESP32 core’s HTTPClient, a different library with a different API.

Built in on Mokxi: #include <ArduinoHttpClient.h> and it builds, with nothing to install. Free, and no account needed.

What ArduinoHttpClient does

You construct an HttpClient with the network client, a bare host name (no http://) and a port. get(), post(), put() and del() send a request in one call; for custom headers you build it in steps with beginRequest(), sendHeader() and endRequest(). Then responseStatusCode() reads the status line and responseBody() reads the rest as a String.

The runnable example on an ESP32-C3 GETs a simulated weather site and POSTs to an echo site, then shows what happens with a host the simulation does not know. Each request goes out as one simulated HTTP request in the Cloud panel, so response headers are not passed back, and the library’s WebSocketClient cannot connect because the network carries no raw TCP.

Library Manager nameArduinoHttpClient
AuthorArduino
Include#include <ArduinoHttpClient.h>
BoardsESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi
In the simulatorRuns, with a gap the note below names.

Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi.

In Mokxi it is built in: #include <ArduinoHttpClient.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ArduinoHttpClient" by Arduino from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ArduinoHttpClient to lib_deps in PlatformIO. Mokxi follows the calls of release 0.6.2, so a sketch written here builds there too.

What to know in the simulator: Each request goes out as one simulated HTTP request, so response headers are not passed back and WebSocketClient cannot connect: Mokxi’s network carries no raw TCP. The WebSockets library’s WebSocketsClient reaches the simulated echo server at echo.mokxi.

The calls you will use

In the order a sketch meets them.

HttpClient client = HttpClient(wifi, "weather.mokxi", 80)

Creates the client on a network Client you declared first, here WiFiClient wifi, with the host name and port.

client.get("/forecast")

Sends a GET for a path on that host. Returns 0 when the request went out, a negative error otherwise.

client.post("/", "application/json", body)

Sends a POST with a content type and a body String or char array. put() takes the same arguments.

client.responseStatusCode()

Waits for and returns the HTTP status, such as 200 or 404, or a negative value when no answer came.

client.responseBody()

Reads the rest of the response and returns it as a String. Call it after responseStatusCode().

client.beginRequest()

Starts a request built in steps, so you can add headers before the body.

client.beginRequest();
client.post("/data");
client.sendHeader("X-Api-Key", "abc123");
client.endRequest();

client.sendHeader("Authorization", "Bearer token")

Adds one header line between beginRequest() and endRequest().

client.del("/item/7")

Sends a DELETE request for the path.

Example: GET the weather and POST to the echo site with HttpClient

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino · ESP32-C3-DevKitM-1
#include <WiFi.h>
#include <ArduinoHttpClient.h>

WiFiClient wifi;
HttpClient weather = HttpClient(wifi, "weather.mokxi", 80);
HttpClient echo = HttpClient(wifi, "echo.mokxi", 80);
HttpClient outside = HttpClient(wifi, "example.com", 80);

void setup() {
  Serial.begin(115200);
  WiFi.begin("Mokxi-Lab", "breadboard");
  while (WiFi.status() != WL_CONNECTED) delay(250);

  weather.get("/");
  Serial.print("weather status ");
  Serial.println(weather.responseStatusCode());

  echo.post("/", "text/plain", "ping");
  Serial.print("echo status ");
  Serial.print(echo.responseStatusCode());
  Serial.print(", body ");
  Serial.println(echo.responseBody());

  Serial.print("example.com: ");
  Serial.println(outside.get("/"));
}

void loop() {}

Open it in the editor, wired and ready to run

Common errors and how to fix them

get() returns -1 and responseStatusCode() is negative

The connection failed. The most common cause is passing a URL like "http://example.com" as the server; the constructor wants only "example.com". Also check WiFi is connected. In Mokxi, only names ending in .mokxi resolve.

Status -3 after a long pause

That is HTTP_ERROR_TIMED_OUT: the server took longer than the default 30 seconds. Raise it with setHttpResponseTimeout(), and check that the server really answers on that port and path from a computer.

An https:// API answers with nothing on port 443

A plain WiFiClient speaks no TLS. On an Uno R4 WiFi or Nano 33 IoT use WiFiSSLClient, and on an ESP32 a WiFiClientSecure with the server’s certificate, then pass that object to HttpClient instead.

Questions and answers

ArduinoHttpClient or the ESP32 HTTPClient?

On an ESP32 alone, the core’s HTTPClient takes a full URL and is a little shorter to use. ArduinoHttpClient is the better choice when the same code must also run on Arduino’s WiFi boards or over Ethernet or cellular.

Try ArduinoHttpClient without installing anything

Open the example, change the sketch, and keep your version in a free account.