Overview of ArduinoHttpClient
masterHost header. Additionally, it simplifies response processing by automatically parsing the HTTP status code and the Content-Length header (when provided).repository·master·Indexed 43 days ago
https://github.com/arduino-libraries/arduinohttpclientA transport-agnostic library for making HTTP requests and establishing WebSocket connections from Arduino-compatible boards. It simplifies interactions with web servers by managing the Host header, parsing status codes and Content-Length, and providing utilities for URL encoding. It requires a separate networking Client library such as WiFiNINA, Ethernet, or MKRGSM to function.
Host header. Additionally, it simplifies response processing by automatically parsing the HTTP status code and the Content-Length header (when provided).Client during initialization. This allows it to work seamlessly with any networking class that inherits from the standard Arduino Client class, including WiFiClient, EthernetClient, and GSMClient.http_parser utility used in this project is a subset of the http_parser library found in the ArduinoCore-mbed repository. It has been modified (shrunk in size) to include only the code necessary for URL parsing, removing unrelated functionality to optimize for size.ArduinoHttpClient does not provide networking itself. It requires a networking hardware module and a corresponding transport-specific Client library. You must include a library that provides a Client instance, such as:
WiFiNINAWiFi101EthernetMKRGSMMKRNBWiFiGSMFor requests requiring custom headers (like Authorization), use the beginRequest() / endRequest() pattern. This allows you to manually inject headers before the body is sent.
client.beginRequest();
client.sendHeader("Custom-Header", "Custom-Value");
client.sendBasicAuth("user", "pass");
client.endRequest();
// If sending a body, use beginBody()
client.beginBody();
client.write("body data");To use HttpClient, you must provide an existing Client instance (such as WiFiClient or EthernetClient), the server name, and optionally the port. The default port is kHttpPort (80).
// Using a string for server name
HttpClient client(wifiClient, "example.com");
// Using a char array for server name
HttpClient client(wifiClient, "example.com", 80);
// Using an IPAddress
HttpClient client(wifiClient, IPAddress(192, 168, 1, 1), 80);You can retrieve the response body in two ways:
responseBody() to get the entire body as a String. This automatically skips headers if they haven't been read.read() to read the body byte-by-byte. This is more memory-efficient for large responses.// Method 1: Get as String
String body = client.responseBody();
// Method 2: Read byte-by-byte
while (client.available()) {
char c = client.read();
// process c
}Use ping() to send a WebSocket ping frame to the server to keep the connection alive or check connectivity. Returns 0 if successful, or an error code.
int status = ws.ping();The get() method connects to the server and sends a GET request to the specified URL path. It returns HTTP_SUCCESS (0) if successful, or an error code if the connection or request fails.
int result = client.get("/api/data");
if (result == HTTP_SUCCESS) {
// Handle success
}After making a request, use responseStatusCode() to get the HTTP status (e.g., 200, 404). To read headers, you must call headerAvailable() in a loop, then use readHeaderName() and readHeaderValue(). This must be done after checking the status code but before reading the body.
int status = client.responseStatusCode();
if (status == 200) {
while (client.headerAvailable()) {
String name = client.readHeaderName();
String value = client.readHeaderValue();
}
}The URLEncoder utility provides static methods to percent-encode strings, making them safe for use in URL query parameters. You can pass either a C-style string (const char*) or an Arduino String object to the encode method. It returns a new String containing the encoded version of the input.
// Using a C-style string
String encodedStr = URLEncoder::encode("hello world!");
// Using an Arduino String object
String myString = "data & more";
String encodedString = URLEncoder::encode(myString);To use WebSockets, instantiate a WebSocketClient by providing an existing Client object (such as a WiFiClient or EthernetClient), the server name (hostname or IP), and the server port. The default port is HttpClient::kHttpPort.
// Example using a WiFiClient
WiFiClient client;
WebSocketClient ws(&client, "example.com", 80);