# ESP8266 HTTP post request return -1 http code

**URL:** <https://forum.arduino.cc/t/esp8266-http-post-request-return-1-http-code/1084107>\
**Category:** Programming\
**Created:** [January 30, 2023, 11:55am UTC](https://forum.arduino.cc/t/esp8266-http-post-request-return-1-http-code/1084107 "2023-01-30T11:55:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sultanzki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sultanzki/32/915604_2.png) [@sultanzki](https://forum.arduino.cc/u/sultanzki)\
**Post date:** [January 30, 2023, 11:55am UTC](https://forum.arduino.cc/t/esp8266-http-post-request-return-1-http-code/1084107/1 "2023-01-30T11:55:57Z")

</div>

I am currently working on ESP8266. I tried to post my data using ESP8266 to my laravel website. I've been trying using postman and my API it is working. But, when I try to post from ESP8266 it is keep show that http code is -1 not 200 (success). This is my code,

```cpp
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
 
void setup() {
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);
  WiFi.begin("ssid", "pass");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
  }
  Serial.print("Connected");
}
 
void loop() {
  int dummy = 100;

  WiFiClient wifiClient;
  HTTPClient http;

  //String json = "{\"device\":\"device_1\",\"suhu_lingkungan\":\"" + String(dummy) + "\",\"kelembaban_lingkungan\":\"" + String(dummy) + "\",\"kelembaban_tanah\":\"" + String(dummy) + "\"}";
  String formData = "device=device_1&suhu_lingkungan=" + String(dummy) + "&kelembaban_lingkungan=" + String(dummy) + "&kelembaban_tanah=" + String(dummy);

  http.begin(wifiClient, "http://127.0.0.1:8000/api/data");

  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  int httpCode = http.POST(formData);
  Serial.println(httpCode);
  String response = http.getString();
  Serial.println(response);
  http.end();
  delay(5000);
}

```

```cpp
I am trying to change my ESP8266 device, change my WiFi, and trying to send dummy data. But, its still not working. Any solution? Thanks!
```

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [January 30, 2023, 3:04pm UTC](https://forum.arduino.cc/t/esp8266-http-post-request-return-1-http-code/1084107/2 "2023-01-30T15:04:49Z")

</div>

If the returning value is -1 it means "HTTPC\_ERROR\_CONNECTION\_FAILED" (check [ESP8266HTTPClient.cpp](https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp) and [ESP8266HTTPClient.h](https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h)).  
You should check what http.begin() returns, if "false" either your server is not active or not reachable. Check also WiFi.status() before trying to connect, just in case the network has been disconnected.

---

<div class="post-metadata">

**Author:** ![noweare](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noweare/32/987856_2.png) [@noweare](https://forum.arduino.cc/u/noweare)\
**Post date:** [July 13, 2023, 1:06pm UTC](https://forum.arduino.cc/t/esp8266-http-post-request-return-1-http-code/1084107/3 "2023-07-13T13:06:03Z")

</div>

Shouldn't your esp32 be the server and your webpage the client ? The the client will request  
the data and the esp32 will respond to the request ?

---

<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:** [January 9, 2024, 1:06pm UTC](https://forum.arduino.cc/t/esp8266-http-post-request-return-1-http-code/1084107/4 "2024-01-09T13:06:08Z")

</div>

Closed on January 9, 2024, 1:06pm UTC

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