Home / References / ESP32 Library / HTTPClient
Description
The GET
method in the ESP32 HTTPClient Library is a powerful tool for retrieving data from a specified web resource using the HTTP protocol. Designed for the ESP32 platform within the Arduino ecosystem, this method simplifies the process of sending HTTP GET requests, making it ideal for IoT projects that require fetching data from servers—think weather updates, sensor dashboards, or DIY automation systems. Whether you’re a hobbyist or a STEM innovator, mastering this method opens up a world of possibilities for connected devices.
Syntax and Usage
The GET
method is invoked on an HTTPClient
object after initializing a connection with begin()
. Below is the basic syntax:
int httpCode = http.GET();
This method can be used in a straightforward manner without additional arguments. Here’s how it works:
- Basic GET Request (No Arguments): Initiates an HTTP GET request to the URL specified in
begin()
. This is the standard usage for fetching data from a server without passing extra parameters in the request itself.
Note: While the GET
method itself doesn’t take arguments, the URL passed to begin()
can include query parameters (e.g., http://httpbin.org/get?avantmaker=HelloEsp32) to customize the request, though this is handled outside the GET
method call. If you need more detailes about this application, please refer to the second example code on our website’s ESP32 HTTPClient Library – begin() method page.
Argument(s)
This method does not require any arguments. The GET
method relies on the URL and headers configured earlier in the HTTPClient
object setup (via begin()
and optional methods like addHeader()
). All necessary request details are predefined, keeping the method call clean and simple.
Return Value
The GET
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 examples demonstrating the usage of the GET
method.
Example 1: Basic GET Request to Fetch Data from www.example.com
This code connects an ESP32 to a WiFi network and performs a GET request to www.example.com
. It prints the HTTP response code and the response payload to the Serial Monitor. Use this as a starting point for fetching data in your projects.
Upload this code to your ESP32 using the Arduino IDE after installing the ESP32 core. Replace YOUR_SSID
and YOUR_PASSWORD
with your WiFi credentials. Open the Serial Monitor (115200 baud) to see the response from www.example.com
. The ESP32 will fetch data every minute, perfect for testing or monitoring applications.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's get method to perform
* a GET request to www.example.com. It prints the HTTP response
* code and the response payload to the Serial Monitor.
*
* 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>
// Replace with your WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Wait for WiFi connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
HTTPClient http;
// Specify the URL
http.begin("http://www.example.com");
// Send GET request
int httpCode = http.GET();
// Check the response
// if (httpCode == 200) {
if (httpCode > 0) {
Serial.printf("HTTP GET successful, code: %d\n", httpCode);
String payload = http.getString();
Serial.println("Response:");
Serial.println(payload);
} else {
Serial.printf("HTTP GET failed, error: %d\n", httpCode);
}
// End the connection
http.end();
// Wait before the next request
delay(60000); // 1 minute
}
Example 2: Include query parameters directly in the URL
This code demonstrates the ESP32 HTTPClient
Library’s GET()
method to send an HTTP GET request to a server. It connects to a Wi-Fi network and retrieves data from a specified URL that includes query parameters, such as “avantmaker=HelloEsp32”. The response from the server is then printed to the Serial Monitor, showing how the GET()
method works with a URL containing predefined parameters.
To use this code, replace “your_SSID” and “your_PASSWORD” with your Wi-Fi credentials. Upload the code to your ESP32 board and open the Serial Monitor to view the connection process and server response. You can modify the serverUrl
variable to include different query parameters or target a different server endpoint.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This code demonstrates the ESP32 HTTPClient
* Library's get() method and how to include query
* parameters directly in the URL.
*
* 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>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Target URL with query parameters (using httpbin.org/get)
const char* serverUrl = "http://httpbin.org/get?avantmaker=HelloEsp32";
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to Wi-Fi!");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
// Make the HTTP GET request
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Specify the URL with query parameters
http.begin(serverUrl);
// Send the GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.printf("HTTP GET request code: %d\n", httpResponseCode);
// Read and print the response
String payload = http.getString();
Serial.println("Response payload:");
Serial.println(payload);
} else {
Serial.printf("Error in HTTP GET request: %d\n", httpResponseCode);
}
// Free resources
http.end();
}
}
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!