# HTTP response code with -1

**URL:** <https://forum.arduino.cc/t/http-response-code-with-1/1243052>\
**Category:** Networking, Protocols, and Devices\
**Created:** [April 2, 2024, 3:48pm UTC](https://forum.arduino.cc/t/http-response-code-with-1/1243052 "2024-04-02T15:48:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobby0bash](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bobby0bash/32/1047748_2.png) [@bobby0bash](https://forum.arduino.cc/u/bobby0bash)\
**Post date:** [April 2, 2024, 3:48pm UTC](https://forum.arduino.cc/t/http-response-code-with-1/1243052/1 "2024-04-02T15:48:01Z")

</div>

```arduino
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "Try";
const char* password = "k@li12345678";

void setup() {
  pinMode(D8, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
    Serial.println(WiFi.status());
  }

  Serial.println("Connected to WiFi");
}

void loop() {
  // Make HTTP GET request to server
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client; // Create a WiFiClient object
    HTTPClient http;
    http.begin(client, "http://10.10.193.226:5000/led"); // Replace with your server IP address and port
    int httpCode = http.GET();

    StaticJsonDocument<200> doc; // Define the JSON document
    
    if (httpCode > 0) {
      String payload = http.getString();
      DeserializationError error = deserializeJson(doc, payload);
      if (error) {
        Serial.print("deserializeJson() failed: ");
        Serial.println(error.c_str());
        return;
      }

      signed char sensor = doc["led_state"];

     if(sensor == 1){
      digitalWrite(D8, HIGH);
     }
     if(sensor == 0){
      digitalWrite(D8, LOW);
     }

      Serial.println(sensor); // Print server response
    } else {
      Serial.println("Error on HTTP request");
      Serial.println(httpCode);
    }

    http.end();
  }

  delay(1000); // Wait 5 seconds before making the next request
}

```

My server is running fine. It is reachable from my Web Browser and Mobile browser.

Wifi also connected but still it responding with -1

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [April 2, 2024, 5:29pm UTC](https://forum.arduino.cc/t/http-response-code-with-1/1243052/2 "2024-04-02T17:29:57Z")

</div>

I moved your topic to an appropriate forum category @bobby0bash.

In the future, please take some time to pick the [forum category](https://forum.arduino.cc/categories) that best suits the subject of your topic. There is an " **About the \_\_\_\_\_ category**" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in [the " **How to get the best out of this forum**" guide](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966). The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

---

<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:** [April 3, 2024, 8:27am UTC](https://forum.arduino.cc/t/http-response-code-with-1/1243052/3 "2024-04-03T08:27:37Z")

</div>

> [@bobby0bash](#):
>
> still it responding with -1

For that -1 returned value the library you are using says:

```arduino
/// HTTP client errors
#define HTTPC_ERROR_CONNECTION_FAILED (-1)

```

So it looks like your ESP (which model exactly?) can't somehow connect to that address.

Enable debug print to serial to be able to see why the http.begin() failed:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/9/4/79473420451deaf2c008065088783d451f7e8baf.png)

---

<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:** [September 30, 2024, 8:27am UTC](https://forum.arduino.cc/t/http-response-code-with-1/1243052/4 "2024-09-30T08:27:57Z")

</div>

Closed on September 30, 2024, 8:27am UTC

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