a simple question of post a data to localhost with nodemcu

hi guys.
i want to send a simple String ("hello world") with nodemcu to my localhost (that created with xamp).

the problem is that i get -1 http code that means localhost didnot get data and in localhost htdoc that data didnot created.

so this is my code:
//*********************************************************
nodemcu:

// For wifi and http connection
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <HttpClient.h>

const char* ssid = "******";
const char* password = "********";
const char* post_link = "http://localhost/Nodemcu/test.php";
String postData;

void setup()
{

  Serial.begin(115200);

  // --------------------------------------------------------  status of connection to wifi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

//-------------------------------------------------------------------- send hello world to localhost
  send_to_server("hello world");
}


void loop()
{
  
}

// -------------------------------------------------------------- send function
void send_to_server(String postData) {
  HTTPClient http;    //Declare object of class HTTPClient
  if (WiFi.status() == WL_CONNECTED) 
  { //Check WiFi connection status
    // Send and get Data
    http.begin(post_link);              //Specify request destination
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
    int httpCode = http.POST(postData);   //Send the request
    Serial.println(httpCode);   //Print HTTP return code
    http.end();  //Close connection
    Serial.println(postData);
  }
  
  else 
  {
    Serial.println("Error in WiFi connection");
  }
  delay(500); // interval

}

//************************************************************
php code in xamp htdoc (test.php)

<?php
$time = time();
$postData = $_POST["postData"];
$file = 'postData.html';
$data = $time."  -  ".$postData;
file_put_contents($file, $data);
?>

You are saying the content is 'application/x-www-form-urlencoded' but you are sending a String.

Hare you tried just sending a simple string as plain text? Does that work? link

okay. To ensure i exactly copy the code of the link and edit it and upload it to my nodemcu. but still i get -1.
where is the problem.

xamp is run and apache is on.

my nodemcu code: ----------------------------------------------------------------------

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

void setup() {

  Serial.begin(115200);                                  //Serial connection
  WiFi.begin("******", "********");   //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

    http.begin("http://localhost");      //Specify request destination
    http.addHeader("Content-Type", "text/plain");  //Specify content-type header

    int httpCode = http.POST("Message from ESP8266");   //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

}

my php code in xamp htdocs: ----------------------------------------------------------------------

<?php
$data = json_encode($_POST);
$file = 'result.txt';
file_put_contents($file, $data);

?>

Can another device connect to localhost?

blh64:
Can another device connect to localhost?

i can connect with my laptop browser.
pls someone tell what is the problem.

In this case, 'localhost' is your Arduino, not your PC.

Find the address of the PC and replace localhost by the IP address.

As an example

//http.begin("http://localhost");      //Specify request destination
http.begin("http://192.168.123.123");      //Specify request destination

Your xampp needs to be listening on the given IP address, not on localhost.
I suppose that you currently try something in the browser like http://localhost. What happens when you use http://192.168.1.37 ? Can you connect? If not, you'll need to reconfigure xampp.

Do the xampp logs give any indication?

But I'm further not familiar with Arduino/NodeMCU and posting to web servers so probably out of here.

sterretje:
In this case, 'localhost' is your Arduino, not your PC.

Find the address of the PC and replace localhost by the IP address.

As an example

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

http.begin("http://192.168.123.123");      //Specify request destination

okay. i test what u said but still it get me -1.
i am using just a nodemcu and not any arduino.

this is my nodemcu code:

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

void setup() {

  Serial.begin(115200);                                  //Serial connection
  WiFi.begin("******", "*******");   //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

    http.begin("http://192.168.*.**");      //Specify request destination
    http.addHeader("Content-Type", "text/plain");  //Specify content-type header

    int httpCode = http.POST("Message from ESP8266");   //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 my php in localhost:

<?php
$data = json_encode($_POST);
$file = 'result.txt';
file_put_contents($file, $data);

?>

one point is that when i search my ip in browset, in htdocs php creat a txt file as "result" that means the localhost side is okay but nodemcu cannot send.
what is the problem?

another thing i test that i replace "http.begin("http://192.168..**")" to another real server "http.begin("http://***.ir/test)" and it didnot work too.

i think there is a problem with my nodemcu but i donot know how to test it that it work properly or not in posting data.
it connect to wifi but cannot send data!

Forget about the nodemcu for now. Use a normal browser.

sterretje:
I suppose that you currently try something in the browser like http://localhost. What happens when you use http://192.168.1.37 ? Can you connect? If not, you'll need to reconfigure xampp.

Do the xampp logs give any indication?

I will add the you might want to create an index.html with some basic text in there.