-->
Page 1 of 1

Posting data to a website using NodeMCU

PostPosted: Tue May 21, 2019 3:40 pm
by rborrasca
Hi,

I'm trying to post some data into a website using ESP8266.
I've searched over the internet for some sketch that would do that, and found this one:

void post_data()
{
HTTPClient http; //Declare object of class HTTPClient

String data = "value=22";
http.begin(URL); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header

int httpCode = http.POST(data); //Send the request
String payload = http.getString(); //Get the response payload

Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload

http.end(); //Close connection

delay(5000); //Post Data at every 5 seconds
}


The problem is that I used this sketch, it didn't worked and I don't know why. httpCode is supposed to return 200 or 201 (I think so, not really sure) but it actually returns -1. And when I print payload, it doesn't print anything, only a blank space.

I can give any other specification you need about the code or even the website, just ask.

Re: Posting data to a website using NodeMCU

PostPosted: Thu May 23, 2019 3:39 pm
by QuickFix
Are you sure the server accepts the form-data url-encoded instead of multipart?

Re: Posting data to a website using NodeMCU

PostPosted: Thu May 23, 2019 4:57 pm
by Bonzo
Is your site a https site? If so it probably will not work.

I am using the same code on a website without problems so it works - do you have an error in your website log.