# Error sending POST, code: -1

**URL:** <https://forum.arduino.cc/t/error-sending-post-code-1/1156843>\
**Category:** Microcontroladores\
**Created:** [August 10, 2023, 2:36am UTC](https://forum.arduino.cc/t/error-sending-post-code-1/1156843 "2023-08-10T02:36:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![serbra](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@serbra](https://forum.arduino.cc/u/serbra)\
**Post date:** [August 10, 2023, 2:36am UTC](https://forum.arduino.cc/t/error-sending-post-code-1/1156843/1 "2023-08-10T02:36:13Z")

</div>

No funciona POST en Nodecu ESP8622, en el siguiente código:  
La respuesta de response\_code siempre es -1  
Alguien puede ayudar para saber a qué se debe?

```arduino

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

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

const char* ssid = "VI";
const char* password = "A33";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

// Define Static IP Setting

IPAddress ip(100,100,1,215);
IPAddress gateway(100,100,1,1);
IPAddress subnet(255,255,0,0);

//Variables used in the code
unsigned int Actual_Millis, Previous_Millis;
int refresh_time = 10000; //Refresh rate of connection to website (recommended more than 1s)

void setup() {
  Serial.begin(115200);

  // prepare LED
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  WiFi.config(ip,gateway,subnet);

  while (WiFi.status() != WL_CONNECTED) {
    delay(10000);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));

  // Start the server
  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {  
  //We make the refresh loop using millis() so we don't have to sue delay();
  Actual_Millis = millis();
  if(Actual_Millis - Previous_Millis > refresh_time){
    Previous_Millis = Actual_Millis;  
    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status 

  Serial.println();
  Serial.println(F("WiFi connected"));

WiFiClient client;
HTTPClient http; 
String dato = "hola" ;
            
      http.begin(client,"http://www.httpbin.org/get"); //Indicate the destination webpage 
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
           
      Serial.print("Dato a enviar : ");
      Serial.println(dato);
      int response_code = http.POST(dato); //Send the POST. This will giveg us a response code
      String msgsrv = http.getString();
   
      Serial.print("Respuesta POST : ");   
      Serial.println(response_code);
      Serial.print("Respuesta del SRV : ");
      Serial.println(msgsrv);
      
      //If the code is higher than 0, it means we received a response
      if(response_code > 0){
        Serial.println("HTTP code " + String(response_code)); //Print return code
  
        if(response_code == 200){ //If code is 200, we received a good response and we can read the echo data
          String response_body = http.getString(); //Save the data comming from the website
          Serial.print("Server reply: "); //Print data to the monitor for debug
          Serial.println(response_body);
     
          }  
        }//End of response_code = 200
      }//END of response_code > 0
      
      else{
       Serial.print("Error sending POST, code: ");
       Serial.println(response_code);
      }
      http.end(); //End the connection
    }//END of WIFI connected
    else{
      Serial.println("WIFI connection error");
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 6, 2024, 2:36am UTC](https://forum.arduino.cc/t/error-sending-post-code-1/1156843/2 "2024-02-06T02:36:46Z")

</div>

Closed on February 6, 2024, 2:36am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
