ESP8266 HTTPcode -1. Error: Connection Refused

Hello everyone,

I am trying to do a http GET request to my web API which is connected to mySQL database. I am able to do a GET request on postman, but unable to do so via ESP8266. I am receiving this error ESP8266 HTTPcode -1. Error: Connection Refused.

Here is my code.

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

void setup () {
  WiFi.begin("aaa", "bbb");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Waiting for connection");

 // check if below pin is necessary!!! ***********************************
  pinMode(2, OUTPUT);
void loop() {
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    Serial.printf("Wifi Connected\n");
    HTTPClient http;  //Declare an object of class HTTPClient
    http.begin("http://localhost:52393/api/dht11service");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request
    //if (httpCode > 0) { //Check the returning code
     Serial.printf("[HTTP] GET... code: %d\n", httpCode);
     if (httpCode >0) {
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
       // http.writeToStream(&Serial);
    http.end();   //Close connection
  delay(1000);    //Send a request every 5 seconds

If anyone could point out an issue, please help. Thanks!

At this point, try replacing "localhost" with an IP address in the form e.g. of the server on which this function "dht11service" is running. It is likely that localhost is not being resolved as you would apparently hope:

http.begin("http://localhost:52393/api/dht11service");  //Specify request destination

Thanks for your help. You're right! I have resolved it.

Thanks again, it worked for me too! After few hours of digging, finally done! Replacing 'localhost' with a particular host IP address was just a magic :-)