ESP32 - HTTP Request

This tutorial instructs you how to use ESP32 to make HTTP request to web server, API, or Web service. In detail, You will learn:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Basic Concepts of Web Client and Web Server

There are some basic concepts of web such as: web address (URL), hostname, pathname, query string, HTTP Request... You can learn detailed about them in HTTP tutorial

How to Make an HTTP Request

  • Declare request method, HTTP port, hostname, pathname, query string
const int HTTP_PORT = 80; const String HTTP_METHOD = "GET"; // or "POST" const char HOST_NAME[] = "google.com"; // hostname of web server: const String PATH_NAME = "";
  • Declare a web client object
  • Connect to web server
if(client.connect(HOST_NAME, HTTP_PORT)) { Serial.println("Connected to server"); } else { Serial.println("connection failed"); }
  • If connected to server, send HTTP request
// send HTTP request header client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP request header
  • Read the response data from web server
while(client.available()) { // read an incoming byte from the server and print them to serial monitor: char c = client.read(); Serial.print(c); } if(!client.connected()) { // if the server's disconnected, stop the client: Serial.println("disconnected"); client.stop(); }

How to include data into HTTP request

We can send data to the web server by including data into HTTP request. The data format depends on HTTP request method:

  • For HTTP GET request
    • Data can be only sent in query string on the pathname.
  • HTTP POST request
    • Data can be sent NOT ONLY in query string format BUT ALSO any other format such as Json, XML, image ...
    • Data is put in HTTP request body.

Let's learn how to send data in query string format for both HTTP GET and POST

  • Create a query string
int temp = // from sensor int humi = // from sensor String queryString = String("?temperature=") + String(temp) + String("&humidity=") + String(humi);
  • HTTP GET: add query string to pathname
// send HTTP header client.println("GET " + PATH_NAME + queryString + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header
  • HTTP POST: put query string in HTTP body
// send HTTP header client.println("POST " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header // send HTTP body client.println(queryString);
  • For both GET and POST, read the response data from web server
while(client.available()) { // read an incoming byte from the server and print them to serial monitor: char c = client.read(); Serial.print(c); } if(!client.connected()) { // if the server's disconnected, stop the client: Serial.println("disconnected"); client.stop(); }

Complete ESP32 Code for Making HTTP Request

The blow is the complete ESP32 code for making HTTP GET/POST request

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-http-request */

Complete ESP32 Code for Making HTTP GET Request with data

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-http-request */

Complete ESP32 Code for Making HTTP POST Request with data

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-http-request */

Learn More

※ NOTE THAT:

Please note that this tutorial is under development. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES