Back to All Blog Posts
Tutorial EltroNerd Engineering

ESP32 Wifi http get and post method request

To perform HTTP GET and HTTP POST requests on an ESP32, you can use the WiFi and HTTPClient libraries.

ESP32 Wifi http get and post method request

Setup Overview

  • Hardware Requirements:

    • ESP32 board.
    • Access to a Wi-Fi network.
  • Libraries:

    • WiFi.h for managing Wi-Fi connectivity.
    • HTTPClient.h for sending HTTP GET and POST requests.
  • Target URL:

    • Replace the placeholders with your desired server's endpoints.

 

  • Hardware Requirements:

    • ESP32 board.
    • Access to a Wi-Fi network.
  • Libraries:

    • WiFi.h for managing Wi-Fi connectivity.
    • HTTPClient.h for sending HTTP GET and POST requests.
  • Target URL:

    • Replace the placeholders with your desired server's endpoints.
arduino
#include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Server URL
const char* serverName = "http://jsonplaceholder.typicode.com/posts/1"; // Example API

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to Wi-Fi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nConnected to Wi-Fi.");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Start the HTTP GET request
    http.begin(serverName);
    int httpResponseCode = http.GET();

    if (httpResponseCode > 0) {
      String payload = http.getString(); // Get the response payload
      Serial.println("HTTP Response Code: " + String(httpResponseCode));
      Serial.println("Response Payload: " + payload);
    } else {
      Serial.println("Error on HTTP request: " + String(httpResponseCode));
    }

    http.end(); // Free resources
  } else {
    Serial.println("Wi-Fi Disconnected");
  }

  delay(10000); // Wait for 10 seconds before the next request
}
arduino
#include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Server URL
const char* serverName = "http://jsonplaceholder.typicode.com/posts"; // Example API

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to Wi-Fi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nConnected to Wi-Fi.");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Start the HTTP POST request
    http.begin(serverName);
    http.addHeader("Content-Type", "application/json"); // Specify content type

    // JSON payload for the POST request
    String postPayload = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
    int httpResponseCode = http.POST(postPayload);

    if (httpResponseCode > 0) {
      String response = http.getString(); // Get the response payload
      Serial.println("HTTP Response Code: " + String(httpResponseCode));
      Serial.println("Response Payload: " + response);
    } else {
      Serial.println("Error on HTTP request: " + String(httpResponseCode));
    }

    http.end(); // Free resources
  } else {
    Serial.println("Wi-Fi Disconnected");
  }

  delay(10000); // Wait for 10 seconds before the next request
}

3. Key Points

  1. Wi-Fi Connection:

    • Ensure the ESP32 is connected to Wi-Fi before making HTTP requests. Use WiFi.status() to check the connection.
  2. HTTPClient Methods:

    • .GET(): Sends an HTTP GET request.
    • .POST(payload): Sends an HTTP POST request with the specified payload.
    • .addHeader(key, value): Adds headers to the HTTP request.
    • .getString(): Retrieves the response payload as a string.
  3. Error Handling:

    • Check the HTTP response code returned by .GET() or .POST(). Codes in the range 200–299 indicate success.
  4. Content-Type:

    • Always set the appropriate Content-Type header for POST requests (e.g., application/json, application/x-www-form-urlencoded).

4. Testing the Code

  • Use services like jsonplaceholder.typicode.com or your own server for testing.
  • Use tools like Postman to debug server-side behavior.

 

Try this project in your browser

Compile code and simulate hardware output with EltroNerd Cloud IDE.

Launch Cloud IDE