Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By AverageGuy
#86751 I am using the following code:
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 all200
<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.
User avatar
By AverageGuy
#86756
JurajA wrote:I don't see any problem. You have a correct response. Or you want a php advice here?


Actually I didn't get the correct response. The retreived document didn't include any of the post data.

I'll take help anywhere I can get it. I have discovered that using the following makes it work:
Code: Select all   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
   String httpRequestData = "msg=Hello World&api_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";           
   // Send HTTP POST request
   int httpCode = http.POST(httpRequestData);


Apparently it doesn't like Content-Type text-plain, nor does it like Content-Type application/json.

Output from first example:
Code: Select all200
<html>
<head>
<title>ESP8266 Post</title>
</head>
<body>
REQUEST METHOD POST<br>
REQUEST DUMP Array
(
    [msg] => Hello World
    [api_key] => tPmAT5Ab3j7F9
    [sensor] => BME280
    [value1] => 24.25
    [value2] => 49.54
    [value3] => 1005.14
)
<br>
MSG Hello World<br>
TEST Test Post<br>

<table>
<tr>
<td>
</td>
</tr>
</table>


</body>

</html>


I'm not sure why.