How to send a HTTP Basic Authentication request from your ESP32 development board

When you connect your ESP32 board to the internet, you may need to send HTTP Basic Authentication requests.

In this situation, we can rely on the HttpClient library from the Arduino core for ESP32.

Given that, this post shows how you can send a HTTP Basic Authentication request from your ESP32 development board.

Enabling ESP32 Development on Arduino IDE

Before continuing, be sure that you have enable ESP32 Development on Arduino IDE.

After you have done so, you should be able to use the HttpClient library from your Arduino sketch.

Writing the Arduino Sketch to send a HTTP Basic Authentication request from your ESP32 development board

When you want to construct a HTTP request to an endpoint with HTTP Basic Authentication from scratch, there are several steps to follow. However, the HttpClient library had simplified these steps for us.

Given that, let's look at an example Arduino Sketch that sends HTTP Basic Authentication requests to a server endpoint:

#include <HTTPClient.h>
#include <WiFiMulti.h>

const char *AP_SSID = "yourAPSSID";
const char *AP_PWD = "yourAPPwd";

WiFiMulti wifiMulti;
void setup() {
  Serial.begin(9600);

  delay(4000);
  wifiMulti.addAP(AP_SSID, AP_PWD);
  
}

void loop() {

  // Block until we are able to connect to the WiFi access point
  if (wifiMulti.run() == WL_CONNECTED) {
    HTTPClient httpClient;
    
    httpClient.begin("https://httpbin.org/basic-auth/username/password");
    httpClient.setAuthorization("username", "password");

    int statusCode = httpClient.GET();

    if (statusCode > 0) {

      Serial.println("Able to send HTTP request out of the board.");
      
      if(statusCode == HTTP_CODE_OK) {
        Serial.println("Server responded with HTTP status 200.");
        String payload = httpClient.getString();
        Serial.println(payload);
      }
      else {
        Serial.printf("Got HTTP status: %d", statusCode);
        String payload = httpClient.getString();
        Serial.println(payload);
      }
    }
    else {
      Serial.printf("Error occurred while sending HTTP Get: %s\n", httpClient.errorToString(statusCode).c_str());
    }

    // Release the resources used
    httpClient.end();
  }

  delay(3000);
}

So what is the above Arduino Sketch doing?

First, it imports the HTTPClient and WiFiMulti libraries to help with connect to the internet.

After that, we define the SSID and password of the wireless access point that our ESP32 board will connect with. If you are using a different set of wireless credentials, then be sure to change them.

Next up, we create an instance of WiFiMulti.

Inside the setup function

When our ESP32 board runs our setup function, it defines a baud rate of 9600 for the board's serial connection. After that, it waits for 4 seconds before adding the wireless credentials to wifiMulti.

Inside the loop function

After setting up the WiFi connection, our ESP32 board will run the loop function.

Whenever the loop function is ran, we call wifiMulti.run() to establish a connection to the wireless network. At this point in time, code execution will stall until our ESP32 board is able to connect to the wireless network.

When the connection is established successfully, the code will attempt to send the HTTP Basic Authentication request.

In order for that to happen, we first create an instance of HTTPClient. After that we include the full URL of the server endpoint with a call to httpClient.begin. In case you are wondering, the URL will reach a server endpoint for testing out basic authentication requests with (username, password) as the credentials.

After that we call httpClient.GET to send the HTTP request with GET as the request method. When the call returns, we will get the status code of the HTTP response that the server returns back to us.

At this point in time, we will know whether the HTTP request had been sent out successfully. When we get a positive number, we know that the server has returned us with a response.

If statusCode == HTTP_CODE_OK, then we proceed to print the response body out to serial. However, if statusCode is not HTTP_CODE_OK, we print out the status code that we have received. When you change the credentials in the URL, you should be able to see this condition block being executed.

In case statusCode is negative, we print the error message to serial. After the checks on statusCode, we call httpClient.end to release the resources being used.

Finally, the last statement of the loop function delays the execution of the loop by 3 seconds.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.