[HTTP-Client] failed to connect to localhost

Hello,
I'm working on a project to connect an node mcu esp8266 to wifi and then execute a post request of a website hosted on localhost. I managed to connect the esp8266 to wifi but the post request always end up failing.

this is the sketch I'm using :

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
 WiFiClient Wificlient; 
void setup() {
 
  Serial.begin(9600);                 //Serial connection
  WiFi.begin("AndroidAP1234", "00224455");   //WiFi connection
 
  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
 
    delay(500);
    Serial.println("Waiting for connection");
 
  }
 
}
 
void loop() {
 
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 
    HTTPClient http;    //Declare object of class HTTPClient
 String url = "http://127.0.0.1/Nodemcu_db_record_view/test1.php";
  http.begin(Wificlient,url);      //Specify request destination
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");  //Specify content-type header
 
   int httpCode = http.POST("valeur=0");   //Send the request
    String payload = http.getString();                  //Get the response payload
 
    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload
 
    http.end();  //Close connection
 
  } else {
 
    Serial.println("Error in WiFi connection");
 
  }
 
  delay(30000);  //Send a request every 30 seconds
 
}

and this is the log I'm getting :

[HTTP-Client][begin] url: http://127.0.0.1/Nodemcu_db_record_view/test1.php
[HTTP-Client][begin] host: 127.0.0.1 port: 80 url: /Nodemcu_db_record_view/test1.php
[HTTP-Client][sendRequest] type: 'POST' redirCount: 0
[HTTP-Client] failed connect to 127.0.0.1:80
[HTTP-Client][returnError] error(-1): connection failed
[HTTP-Client][end] tcp is closed
-1

[HTTP-Client][end] tcp is closed

Can you please help me?
Thanks

@soumiail, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

127.0.0.1 is the IP of the localhost. So your code (as it currently stands) needs to run on the same system as where the web server is running.

If the web server is on e.g. your PC and you want to connect to it, you need to use the IP address of the PC.
If the web server is on the NodeMCU and you want to connect from a browser on e.g. the PC or phone, you will need to use the IP address of the NodeMCU in the browser.

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