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);
  • and the value of host?
    – Juraj
    Jun 3, 2019 at 5:37
  • client is an instance of the WiFiClient object and host is a variable declared to the url with my private key included. Jun 3, 2019 at 19:30
  • error code 400 is "Bad request" and it is a HTTP response from the server
    – Juraj
    Jun 3, 2019 at 19:42
  • Right, but I'm trying to figure out what the inconsistency is between my code and the API tester I used. The JSON and URL are identical. Jun 3, 2019 at 19:43
  • only other thing you provide is host in sketch
    – Juraj
    Jun 3, 2019 at 19:43

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! Jun 4, 2019 at 18:14
  • I appreciate the explanation of what went wrong, but how can we make it work?
    – jezmck
    Sep 11, 2021 at 20:32
  • 1
    @jezmck in this case it would be using the WiFiSecureClient as shown in the BasicHttpsClient example
    – Juraj
    Sep 12, 2021 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.