2

I am trying to make a POST request with NodeMCU, but I get a HTTP status code -1.

Code:

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "Wifi" 
#define STAPSK  "123"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "https://laimonasjanutenas.000webhostapp.com/wifidata.php";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println("trying to post ");

  HTTPClient http;

  http.begin(host);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  int httpCode = http.POST("temp=1337");   
  Serial.println(httpCode);
  http.end();
}

Serial monitor outputs this:

connecting to Wifi
........
WiFi connected
trying to post 
-1

I can confirm that I can actually make a POST to the server. I tested in Postman using this:

https://laimonasjanutenas.000webhostapp.com/wifidata.php?temp=159 

I have no clue what I am doing wrong. Feel free to make POST requests to the this server if needed. Thank you.

  • what status were you expecting? .... why do you say that you are doing something wrong? – jsotola May 21 '19 at 21:37
  • 1
    Does it work if you use http instead of https? It looks like extra params are required for https: github.com/esp8266/Arduino/issues/1941 – Chris Combs May 21 '19 at 21:50
  • 2
    Yes it works! I changed the link from https to http and it worked :). Thanks – katinas May 22 '19 at 9:13
  • 2
    To elaborate on @ChrisCombs comment, you have work to do if you want to talk HTTPS. Please read : forum.arduino.cc/index.php?topic=515541.0 – Greycon Jun 18 '19 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.