-->
Page 1 of 1

How to send HTTP Post from ESP8266

PostPosted: Fri Jun 09, 2017 9:33 pm
by mareyes
Halo. I've bought a new esp8266 Wifi, and I need for make a school project that I need send a HTTP Post... But I got some problems with this, because:


Code: Select all#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

const String Cadenadatos = "info=Servetumamaesmia|06/09/2017 19:27 |25\r\n" ;
const String Tamano = String(Cadenadatos.length()) ;
const String urlfinal = "POST /wsCo2Keellingv1002/wsCo2Kelling.asmx/GuardarInfoArduino\r\nHTTP/1.1\r\nHost:187.201.44.110\r\nContent-Type:application/x-www-form-urlencoded\r\n\r\n"+String(Cadenadatos)+"\r\n" ;

void setup()
{

    USE_SERIAL.begin(9600);
   // USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFiMulti.addAP("Putostodosmenosyo", "CetriX.2016%Gdl");
    delay(2000) ;
/*    if(WiFi.status() != available())
    {

      USE_SERIAL.print("Conectado a la red") ;
    }
    else
    USE_SERIAL.println("No se pudo conectar a la red") ;*/
}

void loop()
{
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
        http.begin("co2kelling.hopto.org"); //HTTP

        USE_SERIAL.print("POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardarInfoArduino HTTP/1.1\r\n") ;
        //USE_SERIAL.print("") ;
        //USE_SERIAL.print("");
        //USE_SERIAL.print("HTTP/1.1\r\n") ;
        USE_SERIAL.print("Host:187.248.44.110\r\n");
        USE_SERIAL.print("Content-Type: application/x-www-form-urlencoded\r\n");
        USE_SERIAL.print("Content-Length: "+String(Tamano)+"\r\n");
        USE_SERIAL.print("\r\n") ;
        USE_SERIAL.print("info="+String(Tamano)+"\r\n") ;
//        USE_SERIAL.end() ;

        //USE_SERIAL.print("GuardarInfoArduino HTTP/1.1\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

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

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

        http.end();
    }

    delay(10000);
}


I want to send a HTTP POST NOT a HTTP GET but I recevied response the next thing:

[HTTP] begin...
POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardarInfoArduino HTTP/1.1
Host:187.248.44.110
Content-Type: application/x-www-form-urlencoded
Content-Length: 44

info=44
[HTTP] GET... failed, error: connection refused


I tried to change the code part:

Code: Select all        int httpCode = http.GET();


to "http.POST();"

But at time of compile I received of error:


/media/mareyes/EE73-2845/asdasd/asdasd.ino: In function 'void loop()':
asdasd:73: error: no matching function for call to 'HTTPClient::POST()'
int httpCode = http.POST();
^

/media/mareyes/EE73-2845/asdasd/asdasd.ino:73:34: note: candidates are:
In file included from /media/mareyes/EE73-2845/asdasd/asdasd.ino:12:0:
/home/mareyes/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h:157:9: note: int HTTPClient::POST(uint8_t*, size_t)
int POST(uint8_t * payload, size_t size);
^
/home/mareyes/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h:157:9: note: candidate expects 2 arguments, 0 provided
/home/mareyes/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h:158:9: note: int HTTPClient::POST(String)
int POST(String payload);
^
/home/mareyes/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h:158:9: note: candidate expects 1 argument, 0 provided
exit status 1
no matching function for call to 'HTTPClient::POST()'

Re: How to send HTTP Post from ESP8266

PostPosted: Tue Jun 13, 2017 1:25 pm
by gbafamily1
See the example below.

viewtopic.php?f=29&t=14053

Re: How to send HTTP Post from ESP8266

PostPosted: Wed Jun 14, 2017 7:03 am
by QuickFix
HTTP <> HTTPS: that's why you're getting the "Connection refused" message.

[EDIT] Sorry... was looking at the wrong line in your code. :oops:

use http.begin() with full path, including the "http://"-prefix and page "/blahblah.asmx"-suffix and only POST the parameters using http.post().