ESP32 HTTPClient Library – PATCH

Home / References / ESP32 Library / HTTPClient

Description

The PATCH method is a powerful tool for making partial updates to a resource on a server. Whether you’re tweaking IoT device settings or updating specific data fields, this method allows you to send targeted changes over HTTP/HTTPS, making it an essential part of your ESP32 toolkit for efficient and precise communication.


Syntax and Usage

The PATCH method in the ESP32 HTTPClient Library is used to send a PATCH request to a specified server. Below is the basic syntax, followed by the different ways it can be applied:

int httpCode = http.PATCH(payload);

The PATCH method can be used in the following ways:

  • With a String payload: Send data as a String object to the server. Ideal for simple updates or when working with text-based data like JSON.
  • With a byte array payload: Send raw data as a byte array with a specified length. Useful for binary data or custom formats requiring precise control.

Argument(s)

The PATCH method requires one argument, which defines the data to be sent in the request. Depending on the usage, the argument can vary:

  • payload (String): A String containing the data to be sent to the server. This is typically formatted as JSON, XML, or plain text, depending on the server’s requirements.
  • payload (uint8_t* data, size_t len): A pointer to a byte array (uint8_t*) and its length (size_t). This allows you to send raw binary data or any custom format.

Return Value

The PATCH method returns an integer representing the HTTP response code from the server. Common values include:

  • 200: Success (OK)
  • 400: Bad Request (invalid data or syntax)
  • 404: Not Found (server endpoint unavailable)
  • Negative values: Error codes indicating a failure in the request (e.g., connection issues).

Checking this return value is crucial for debugging and ensuring your request was processed correctly.

For a complete list of Return Values please refer to ESP32 HTTPClient Library – HTTP Status Codes and Error Codes


Example Codes

Below are example codes demonstrating each usage of the PATCH method. These examples connect to www.httpbin.org, a handy testing service for HTTP requests, to showcase real-world application.

Example 1: PATCH with a String Payload

This example sends a JSON string to update a resource on httpbin.org. It’s perfect for IoT projects needing to update specific data fields, like sensor statuses.

/*
 * Author: Avant Maker
 * Date: February 23, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's PATCH method to update a 
 * resource on httpbin.org. 
 *
 * License: MIT 
 * 
 * Code Source: This example code is sourced from the Comprehensive 
 * Guide to the ESP32 Arduino Core Library, accessible on 
 * AvantMaker.com. For additional code examples and in-depth 
 * documentation related to the ESP32 Arduino Core Library, 
 * please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things 
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated 
 * to empowering makers, learners, and enthusiasts with the resources
 * they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "your-SSID";
const char* password = "your-PASSWORD";

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected to WiFi");

    HTTPClient http;
    http.begin("http://httpbin.org/patch");
    http.addHeader("Content-Type", "application/json");

    String payload = "{\"status\": \"active\", \"value\": 42}";
    int httpCode = http.PATCH(payload);

    if (httpCode > 0) {
        Serial.printf("PATCH success, code: %d\n", httpCode);
        String response = http.getString();
        Serial.println(response);
    } else {
        Serial.printf("PATCH failed, error: %d\n", httpCode);
    }
    http.end();
}

void loop() {
    // Empty loop
}

Example 2: PATCH with a Byte Array Payload

This example sends raw binary data as a byte array. It’s useful for scenarios where you need to send non-text data, such as firmware updates or custom binary protocols.

/*
 * Author: Avant Maker
 * Date: February 23, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's PATCH method to send 
 * raw binary data as a byte array. 
 *
 * License: MIT 
 * 
 * Code Source: This example code is sourced from the Comprehensive 
 * Guide to the ESP32 Arduino Core Library, accessible on 
 * AvantMaker.com. For additional code examples and in-depth 
 * documentation related to the ESP32 Arduino Core Library, 
 * please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things 
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated 
 * to empowering makers, learners, and enthusiasts with the resources
 * they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "your-SSID";
const char* password = "your-PASSWORD";

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected to WiFi");

    HTTPClient http;
    http.begin("http://httpbin.org/patch");
    http.addHeader("Content-Type", "application/octet-stream");

    uint8_t payload[] = {0xDE, 0xAD, 0xBE, 0xEF};
    size_t len = sizeof(payload);
    int httpCode = http.PATCH(payload, len);

    if (httpCode > 0) {
        Serial.printf("PATCH success, code: %d\n", httpCode);
        String response = http.getString();
        Serial.println(response);
    } else {
        Serial.printf("PATCH failed, error: %d\n", httpCode);
    }
    http.end();
}

void loop() {
    // Empty loop
}
error: Content is protected !!