So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Sushant Naik
#75637 Hi,

i am trying to use HTTP Post action,

i am using example posted in this https://circuits4you.com/2018/03/10/esp8266-nodemcu-post-request-data-to-website/.

Here is my code,
Code: Select allHTTPClient http;    //Declare object of class HTTPClient
// ------------------------------URL---------------------------Port-------uri
httpCode = http.begin("https://www.sappush.com", 3000, "api1/devices/10/setWifi");

http.addHeader("Content-Type", "application/json");  //Specify content-type header

httpCode = http.POST("token:1234567890abc");   //Send the request

 if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            Serial.printf("[HTTP] POST... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                Serial.println(payload);
            }
        } else {
            Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }


i get following response,

Code: Select all[HTTP] POST... failed, error: connection refused


i have checked this URL and HTTP POST request from Advanced REST Client Chrome extension,

following is HTML code from APP
Code: Select allPOST /api1/devices/10/setWifi HTTP/1.1
HOST: http://www.sappush.com:3000
content-type: application/json
content-length: 25

{"token":"1234567890abc"}




and the response in APP
Code: Select all{
"Ack": "Ok"
}


But i am doing something wrong in ESP8266 using HTTPCLIENT, i would like to know my mistakes,

Thanks
Sushant
User avatar
By Sushant Naik
#75657
Sushant Naik wrote:Hi,

i am trying to use HTTP Post action,

i am using example posted in this https://circuits4you.com/2018/03/10/esp8266-nodemcu-post-request-data-to-website/.

Here is my code,
Code: Select allHTTPClient http;    //Declare object of class HTTPClient
// ------------------------------URL---------------------------Port-------uri
httpCode = http.begin("https://www.sappush.com", 3000, "api1/devices/10/setWifi");

http.addHeader("Content-Type", "application/json");  //Specify content-type header

httpCode = http.POST("token:1234567890abc");   //Send the request

 if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            Serial.printf("[HTTP] POST... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                Serial.println(payload);
            }
        } else {
            Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }


i get following response,

Code: Select all[HTTP] POST... failed, error: connection refused


i have checked this URL and HTTP POST request from Advanced REST Client Chrome extension,

following is HTML code from APP
Code: Select allPOST /api1/devices/10/setWifi HTTP/1.1
HOST: http://www.sappush.com:3000
content-type: application/json
content-length: 25

{"token":"1234567890abc"}




and the response in APP
Code: Select all{
"Ack": "Ok"
}


But i am doing something wrong in ESP8266 using HTTPCLIENT, i would like to know my mistakes,

Thanks
Sushant



Now its getting connected, i was giving wrong URL,

Code: Select allhttpCode = http.begin("www.sappush.com", 3000, "/api1/devices/10/setWifi");


after changing this line it is getting connected but i am getting error(-3): send payload failed

any help ?