API with Bearer Authentication

-SOLVED

Hi all

I just need to GET a single string onto a Webserver who has supplied me with an APi and Bearer Authentication key.

I have tried http library, Json library but I can’t wrap my head around this one.

I know it works, as I used it with Postman and data is saved on webserver.
How do I implement it on ESP32 ?
Please see the attached HTTP code created by Postman

ARDUINO code based on Rui Santos’s work

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "Botes****";
const char* password = "Bot******";

//Your Domain name with URL path or IP address with path
String serverName = "https://*********.azurewebsites.net/api/SensorData/StoreString?dataString=";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void sendHeader(const char* aHeaderName, const char* aHeaderValue);

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "000003Y";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      http.sendHeader("Authorization: Bearer eyJhbGciOiJIUzI1Ni******************bl9uYW1lIjoiSm9obiBEb2UiLCJyb2xlIjoiR3*********************9lLmNvbSIsIm5iZiI6MTU5ODUzODE**************NDU4MTU0LCJpYXQiOjE1OTg1MzgxNTR9.cBjUCDVW78********************************");
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

POSTMAN CODE

GET /api/SensorData/StoreString?dataString=000003Y HTTP/1.1
Host: bxxxxxxxxxxx.azurewebsites.net
Authorization: Bearer eyJhbGciOiJxxxxxxxxxxxxxxxxxxxxxxCI6IkpXVCJ9.eyJnaXZlxxxxxxxxxxxxxxxxxxxxdWVfbmFtZSI6ImpvaG5AZG9lLmxxxxxxxxxxxxxxxxxxxxxxxxxxxNjI0NDU4MTU0LCJpYXQiOjxxxxxxxxxxxxE1OTg1MzgxNTR9xxxxxxxxxxxxxxxxxxxxxxxxxoIt84Dxfi_ZGszv1KgvyrM

New Text Document.doc (344 Bytes)

If you post your code as described in the how to use this forum sticky, more members will read it.

Thanks for the hint. I have now attached my attempt at code.

  • SOLVED

Hi everyone.
Don’t know what I did different as below code did not work before, but now working.
Hope this helps someone else.

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "XXXXXXXX";
const char* password = "XXXXXXXXXX";

//Your Domain name with URL path or IP address with path
String serverName = "https://xxxxxxxxxxxxxxxxxxxxx.net/api/Sxxxxxxxxx/StoreString?dataString=";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 10000;

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void sendHeader(const char* aHeaderName, const char* aHeaderValue);

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "000003Y";      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      http.addHeader("Authorization","Bearer eyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxJyb2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxSIsIm5iZiI6MTU5ODUxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cBjUxxxxxxxxxxxxxxxxxxx_Zxxxxxxxxxxxxxxxxxx");
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

New Text Document.ino (2.61 KB)

Thank you, you saved my day!
Many forums mention the addHeader doesn’t work. Actually it works like a charm.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.