Cannot get ESP8266 POST to work.
Posted: Sat Apr 25, 2020 6:03 am
I am using the following code:
The php2.php code looks like:
The output in the /dev/ttyUSB0 serial console looks like
Any suggestions would be gladly received. I'll point you to the actual "myserver" in a pm if anyone want's to help.
Thanks,
Jim.
Code: Select all
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200); //Serial connection
WiFi.begin("xxx", "rrrr"); //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://myserver.com/php2.php/"); //Specify request destination
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
int httpCode = http.POST("{\"hello\":\"world\"}"); //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
}
The php2.php code looks like:
Code: Select all
<html>
<head>
<title>ESP8266 Post</title>
</head>
<body>
<?php
{
$r=$_SERVER["REQUEST_METHOD"];
echo "REQUEST METHOD $r<br>\n";
$x=print_r($_REQUEST,true);
$msg="NOT SET";
if(isset($_REQUEST['msg'])) {
$msg= $_REQUEST['msg'];
}
echo "REQUEST DUMP $x<br>\n";
echo "MSG $msg<br>\n";
$myvar = "Test Post";
echo "TEST $myvar<br>\n";
}
?>
<table>
<tr>
<td>
</td>
</tr>
</table>
</body>
</html>
The output in the /dev/ttyUSB0 serial console looks like
Code: Select all
200
<html>
<head>
<title>ESP8266 Post</title>
</head>
<body>
REQUEST METHOD POST<br>
REQUEST DUMP Array
(
)
<br>
MSG NOT SET<br>
TEST Test Post<br>
<table>
<tr>
<td>
</td>
</tr>
</table>
</body>
</html>
Any suggestions would be gladly received. I'll point you to the actual "myserver" in a pm if anyone want's to help.
Thanks,
Jim.