ArduinoHttpClient

repository·master·Indexed 43 days ago

https://github.com/arduino-libraries/arduinohttpclient

A 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.

Tokens
3K
Snippets
16
Records
20
Agent score
56%

What's inside ArduinoHttpClient

  1. Overview of ArduinoHttpClient

    master
    ArduinoHttpClient is a library designed to simplify interactions between Arduino boards and web servers. It manages outgoing HTTP requests and automatically handles the Host header. Additionally, it simplifies response processing by automatically parsing the HTTP status code and the Content-Length header (when provided).
  2. How ArduinoHttpClient works with networking clients

    master
    ArduinoHttpClient is designed to be transport-agnostic. It expects an object of type 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.
  3. Configure dependencies for ArduinoHttpClient

    master

    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:

    • WiFiNINA
    • WiFi101
    • Ethernet
    • MKRGSM
    • MKRNB
    • WiFi
    • GSM
  4. Perform complex HTTP requests with custom headers

    master

    For 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");
  5. Initialize the HttpClient

    master

    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);
  6. Read the HTTP response body

    master

    You can retrieve the response body in two ways:

    1. As a String: Use responseBody() to get the entire body as a String. This automatically skips headers if they haven't been read.
    2. As a Stream: Use 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
    }
  7. Read HTTP response status and headers

    master

    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();
        }
    }
  8. Encode URL parameters with URLEncoder::encode()

    master

    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);
  9. Initialize a WebSocketClient

    master

    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);