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.
//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();
}
}
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
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.
Hi @horace, thanks for the response.
Yes, I have checked that tutorial, I am actually using its Post method in my case as well but the Authorization isn't working for some reason.