ESP32 HTTPClient Library – PUT

Home / References / ESP32 Library / HTTPClient

Description

The PUT method in the ESP32 HTTPClient Library allows you to send an HTTP PUT request to a specified server. This method is typically used to update resources on a web server by replacing the existing content with the data provided in the request payload. It’s an essential tool for IoT applications where an ESP32 needs to upload or modify data on a remote server, making it perfect for DIY projects, STEM experiments, and innovative IoT solutions.


Syntax and Usage

PUT with Payload: 

int httpCode = http.PUT(payload);

Use this to send data (e.g., a string or binary content) to the server to update a resource.

For more details regarding the usage of this method, please refer to the Example Codes section of this page.


Argument(s)

The PUT method can optionally take one argument, depending on the usage:

  • payload (String): The data to be sent to the server in the body of the PUT request. This can be a string containing text, JSON, or any other format supported by the server. If no payload is provided, the method sends an empty request body.
  • payload (uint8_t* data, size_t length):
    A pointer to a byte array (uint8_t*) and its size (size_t). This allows sending raw binary data, offering flexibility for advanced applications.

Return Value

The PUT 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

Example 1: PUT Request with Payload
This example shows how to send a PUT request to httpbin.org/put with a JSON payload. The httpbin.org service echoes back the data sent in the request, making it useful for testing. Here, the ESP32 sends a JSON string, and the response includes the payload for verification.

/*
 * Author: Avant Maker
 * Date: February 21, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's PUT method to send 
 * a PUT request to httpbin.org/put with a JSON payload. 
 *
 * 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>

// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

void setup() {
    Serial.begin(115200);

    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");

    // Initialize HTTPClient
    HTTPClient http;

    // Specify the URL
    http.begin("https://httpbin.org/put");

    // Set content type header
    http.addHeader("Content-Type", "application/json");

    // Define the payload
    String payload = "{\"device\":\"ESP32\",\"status\":\"active\",\"value\":42}";

    // Send PUT request with payload
    int httpCode = http.PUT(payload);

    // Check the response
    if (httpCode > 0) {
        Serial.printf("PUT request succeeded with code: %d\n", httpCode);
        String response = http.getString();
        Serial.println("Server response: " + response);
    } else {
        Serial.printf("PUT request failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    // End the connection
    http.end();
}

void loop() {
    // Do nothing in loop
}

Example 2: send a payload as a byte array with a specified size
Below is a new demo code for the ESP32 HTTPClient Library’s PUT method, specifically using the int PUT(uint8_t *payload, size_t size) variant. This version of the PUT method allows you to send a payload as a byte array (uint8_t *) with a specified size, which is useful for sending raw binary data or non-string payloads.

/*
 * Author: Avant Maker
 * Date: February 21, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's PUT method to send 
 * a payload as a byte array (uint8_t *) with a specified 
 * size, which is useful for sending raw binary data or 
 * non-string payloads.
 *
 * 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>

// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

void setup() {
    Serial.begin(115200);

    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");

    // Initialize HTTPClient
    HTTPClient http;

    // Specify the URL
    http.begin("https://httpbin.org/put");

    // Set content type header (optional, depending on payload type)
    http.addHeader("Content-Type", "application/octet-stream");

    // Define the payload as a byte array
    uint8_t payload[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x45, 0x53, 0x50, 0x33, 0x32}; // "Hello ESP32" in ASCII
    size_t payloadSize = sizeof(payload);

    // Send PUT request with byte array payload
    int httpCode = http.PUT(payload, payloadSize);

    // Check the response
    if (httpCode > 0) {
        Serial.printf("PUT request succeeded with code: %d\n", httpCode);
        String response = http.getString();
        Serial.println("Server response: " + response);
    } else {
        Serial.printf("PUT request failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    // End the connection
    http.end();
}

void loop() {
    // Do nothing in loop
}
error: Content is protected !!