Every time I try the post request I get a 400 error but the JSON and URL are identical to those that I used on an online API tester and it worked every time there. I am running this program in the official Arduino IDE and most of this code was taken from the ESP8266 official library here: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
if (http.begin(client, host)) { // HTTP
http.addHeader("Content-Type", "application/json");
// Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
// int httpCode = http.GET();
String request = "{ \"temperature\": 27.0, \"humidity\": 43 }";
Serial.print("[HTTP] POST...\n");
int httpCode = http.POST(request);
1 Answer 1
400 is a HTTP error "Bad request" sent by the HTTP server. Possible reasons for this error are malformed HTTP request line, malformed HTTP request headers, missing empty line after the last HTTP header and a request to 'secure' port without encryption.
In your case the HttpClient library constructs the HTTP request so there should be no error. Only thing you supply for the request is the URL in your case it is the variable host
. As we cleared in comments, your URL started with https. The HttpClient library parsed the url, detected the https protocol and used the secure port 443. But the underlying networking Client object was not SSL so it didn't encrypt the request.
-
Thank you for your help!pythonhelpthrow– pythonhelpthrow2019年06月04日 18:14:20 +00:00Commented Jun 4, 2019 at 18:14
-
I appreciate the explanation of what went wrong, but how can we make it work?jezmck– jezmck2021年09月11日 20:32:56 +00:00Commented Sep 11, 2021 at 20:32
-
1@jezmck in this case it would be using the WiFiSecureClient as shown in the BasicHttpsClient example2021年09月12日 08:08:01 +00:00Commented Sep 12, 2021 at 8:08
host
?host
in sketch