Skip to content

HTTP POST problem #2678

Closed
Closed
@MohammadMokhalled

Description

@MohammadMokhalled

I have have a problem to send something use POST method and then read the page. my code is below:

    #include <Arduino.h>
    #include <ESP8266WiFi.h>
    #include <ESP8266WiFiMulti.h>
    #include <ESP8266HTTPClient.h>
    #define USE_SERIAL Serial
   
    ESP8266WiFiMulti WiFiMulti;
    HTTPClient http;
 void setup() {
  USE_SERIAL.begin(115200);
 // 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("SSID", "PASSWORD");
 }

      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("http://server/test.php"); //HTTP

    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
     http.POST("id=s1&api=123456789");
     http.writeToStream(&Serial);
     http.end();

    USE_SERIAL.print("[HTTP] GET...\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] GET... 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);}

when i run it i get the 411 error code. whats wrong here? how can i solve it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions