ESP32 http post request using Authorization Bearer
Posted: Wed May 11, 2022 6:17 am
Hi,
I am trying to make an HTTP Post request using Arduino for ESP32. I am using the HTTPClient library to make the Post request in which I have to use the Authorization Bearer token. Below is my code.
I have tried both Content-Type and even without any Content-Type.
My ESP32 is successfully connecting and getting a valid IP Address. After making the Post request, I am getting
I have checked the same Post request using Postman and I am getting the right response. Details of Postman's request are below.
Looking for advice to make it work, maybe it's trivial for experts.
Regards
Sohaib
I am trying to make an HTTP Post request using Arduino for ESP32. I am using the HTTPClient library to make the Post request in which I have to use the Authorization Bearer token. Below is my code.
Code: Select all
//Your Domain name with URL path or IP address with path
const char* serverName = "http://domain.com";
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
//http.addHeader("Content-Type", "text/plain");
//http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Specify Authorization-type header
http.addHeader("Authorization", "Bearer eyJ0eXAiOiJK...");
// Data to send with HTTP POST
String httpRequestData = "/api/v1/cattle/location?cattle_id=1&location_date=2022-05-1015:25:00&latitude=12.1231111&longitude=-151.82723";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
My ESP32 is successfully connecting and getting a valid IP Address. After making the Post request, I am getting
.HTTP Response code: 400
I have checked the same Post request using Postman and I am getting the right response. Details of Postman's request are below.
Looking for advice to make it work, maybe it's trivial for experts.
Regards
Sohaib