Home / References / ESP32 Library / HTTPClient
Description
The sendRequest()
method in the ESP32 HTTPClient Library allows you to send any type of HTTP request (e.g., GET, POST, DELETE, PUT) to a server. Unlike specific methods like GET()
or POST()
, sendRequest()
offers flexibility by letting you define the HTTP method as a string, making it ideal for advanced or custom interactions with web services.
Syntax and Usage
The sendRequest()
method is called on an HTTPClient
object after setting up a connection with begin()
. Below is the basic syntax, followed by its usage variations:
int httpCode = http.sendRequest("METHOD");
Here are the different ways to use the sendRequest()
method:
- Basic Request without Payload: Sends an HTTP request with the specified method (e.g., “GET”, “DELETE”) and no body. Perfect for simple operations like retrieving or deleting resources.
- Request with String Payload: Sends an HTTP request with a payload provided as a
String
object, such as JSON or plain text. Useful for methods like POST or PUT that require data. - Request with Byte Array Payload: Sends an HTTP request with a payload as a byte array and its size. Ideal for sending raw data or binary content.
- Request with Stream Payload: Sends an HTTP request with data streamed from a
Stream
object (e.g., a file). Great for sending large datasets efficiently.
Argument(s)
The sendRequest()
method requires at least one argument and supports optional additional arguments depending on the usage:
- type (const char*): The HTTP method to use (e.g., “GET”, “POST”, “PUT”, “DELETE”). This is a required argument and must be a null-terminated string specifying the request type.
- payload (uint8_t*, optional): A pointer to a byte array containing the request body. Used with the
size
argument to define the data length. Defaults toNULL
if omitted. - size (size_t, optional): The length of the byte array payload in bytes. Required when using a
uint8_t*
payload; defaults to 0 if omitted. - payload (String&, optional): A reference to a
String
object containing the request body. Used as an alternative to the byte array for convenience. - stream (Stream*, optional): A pointer to a
Stream
object (e.g.,File
orSerial
) from which the request body is read. Requires thesize
argument.
Return Value
The sendRequest()
method returns an integer representing the HTTP response code:
- Positive Value (e.g., 200, 201, 204): Indicates success. Common codes include 200 (OK), 201 (Created), or 204 (No Content), depending on the server’s response to the request.
- Negative Value: Indicates an error, such as -1 (connection failed), -11 (timeout), or other library-specific codes. Check the library documentation for a full list of error codes.
Example Codes
Below are example codes for each usage variation of the sendRequest()
method. These connect to www.httpbin.org
, a testing service, to demonstrate practical applications.
Example 1: Basic Request without Payload
This example demonstrates how to use the sendRequest()
method to send a basic HTTP GET request from an ESP32. The ESP32 first connects to a WiFi network, and then it sends a GET request to the endpoint http://httpbin.org/get
. The HTTP response code is printed to the serial monitor, along with the response body. This example helps users understand how to establish a WiFi connection, send a GET request, and handle the response using the ESP32 HTTPClient library.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's sendRequest method to
* send a basic HTTP GET request from an ESP32.
*
* 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/get"); // URL for GET request
int httpCode = http.sendRequest("GET"); // Send basic GET request
if (httpCode > 0) {
Serial.printf("Request succeeded with code: %d\n", httpCode);
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.printf("Request failed with error code: %d\n", httpCode);
}
http.end(); // Free resources
}
void loop() {
// Nothing to do here
}
Example 2: Request with String Payload
This example demonstrates how to use the sendRequest()
method to send an HTTP POST request with a JSON payload from an ESP32. The ESP32 connects to a WiFi network, then sends a POST request to the endpoint http://httpbin.org/post
with a JSON payload containing a simple key-value pair. The request includes a “Content-Type” header to specify that the body is in JSON format. The HTTP response code and the response body are printed to the serial monitor, allowing users to see the outcome of the request.
Attention: To better understand how the ESP32 sends data to a web server and processes the server’s response when running the example code below, please check out the following webpage. It provides detailed information about the POST request data and the responses from the httpbin.org web server.
/*
* Author: Avant Maker
* Date: February 23, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's sendRequest method to
* send an HTTP POST request with a JSON payload from an ESP32.
*
* 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/post"); // URL for POST request
http.addHeader("Content-Type", "application/json"); // Set content type
String payload = "{\"key\":\"value\"}"; // JSON payload
int httpCode = http.sendRequest("POST", payload); // Send POST with String
if (httpCode > 0) {
Serial.printf("Request succeeded with code: %d\n", httpCode);
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.printf("Request failed with error code: %d\n", httpCode);
}
http.end(); // Free resources
}
void loop() {
// Nothing to do here
}
Example 3: Request with Byte Array Payload
This example demonstrates how to use the sendRequest()
method to send an HTTP PUT request with raw text data from an ESP32. After connecting to a WiFi network, the ESP32 sends a PUT request to the endpoint http://httpbin.org/put
with a plain-text payload (“Hello, ESP32!”). The “Content-Type” header is set to “text/plain” to specify that the body content is plain text. The HTTP response code and the response body are printed to the serial monitor, showing the result of the request.
/*
* Author: Avant Maker
* Date: February 23, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's sendRequest method to
* send an HTTP PUT request with raw text data from an ESP32.
*
* 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/put"); // URL for PUT request
http.addHeader("Content-Type", "text/plain"); // Set content type
const char* payload = "Hello, ESP32!";
int httpCode = http.sendRequest("PUT", (uint8_t*)payload, strlen(payload)); // Send PUT with byte array
if (httpCode > 0) {
Serial.printf("Request succeeded with code: %d\n", httpCode);
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.printf("Request failed with error code: %d\n", httpCode);
}
http.end(); // Free resources
}
void loop() {
// Nothing to do here
}
Example 4: Request with Stream Payload
This example demonstrates how to use the sendRequest()
method to send an HTTP POST request with streamed data from an ESP32. After connecting to a WiFi network, the ESP32 sends a POST request to the endpoint http://httpbin.org/post
with the body content coming from a StreamString
object. The “Content-Type” header is set to “text/plain”, and the stream data (“Streamed data from ESP32”) is sent as the body. The HTTP response code and the response body are printed to the serial monitor, showing the result of the request.
/*
* Author: Avant Maker
* Date: February 23, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's sendRequest method
* to send an HTTP POST request with streamed data from
* an ESP32. After connecting to a WiFi network, the ESP32
* sends a POST request to the endpoint http://httpbin.org/post
* with the body content coming from a StreamString object.
* The "Content-Type" header is set to "text/plain", and the
* stream data ("Streamed data from ESP32") is sent as the body.
* The HTTP response code and the response body are printed
* to the serial monitor, showing the result of the request.
*
*
* 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>
#include <StreamString.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/post"); // URL for POST request
http.addHeader("Content-Type", "text/plain"); // Set content type
StreamString stream;
stream.print("Streamed data from ESP32");
int httpCode = http.sendRequest("POST", &stream, stream.available()); // Send POST with stream
if (httpCode > 0) {
Serial.printf("Request succeeded with code: %d\n", httpCode);
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.printf("Request failed with error code: %d\n", httpCode);
}
http.end(); // Free resources
}
void loop() {
// Nothing to do here
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 HTTPClient Library
- Connection
- Request Methods
- Request Config
- Response
- Cookie
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!