How display POST request from ESP32 on webserver

Hi,

I have a NGINX server on a raspberry pi. I want to send a POST request from my ESP32 board to the NGINX web-server.

I use this code I found in a tutorial:

#include <WiFi.h>
#include <HTTPClient.h>
 
const char* ssid = "freebox_FSVWVO";
const char* password = "legendaireinvincible1234";
 
void setup() {
 
  Serial.begin(115200);
  delay(4000);   //Delay needed before calling the WiFi.begin
 
  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");
 
}
 
void loop() {
 
 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
 
   HTTPClient http;   
 
   http.begin("http://192.168.20.55:150/postfile/receive.php");  //Specify destination for HTTP request
   //http.begin("http://jsonplaceholder.typicode.com/posts");  //Specify destination for HTTP request
   http.addHeader("Content-Type", "text/plain");             //Specify content-type header
 
   int httpResponseCode = http.POST("test=POSTING from ESP32");   //Send the actual POST request
 
   if(httpResponseCode>0){
 
    String response = http.getString();                       //Get the response to the request

    Serial.println("Return code");
    Serial.println(httpResponseCode);   //Print return code
    Serial.println("Request answer");
    Serial.println(response);           //Print request answer
 
   }else{
 
    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
 
   }
 
   http.end();  //Free resources
 
 }else{
 
    Serial.println("Error in WiFi connection");   
 
 }
 
  delay(10000);  //Send a request every 10 seconds
 
}

On my webserver, I have created a php file (receive.php) to diplay the POST request thrown from my ESP32 :

<html>
<body>

Text received :  <?php echo $_POST['test']; ?>


</body>
</html>

What I get on my serial monitor is :

Return code
200
Request answer
<html>
<body>

Text received :  


</body>
</html>

I don't know how to display the POST request from ESP32 on my webserver. Could you help me ?

Thank you,

Save it to a file and then create a web page that displays this file.

Thanks a lot your help.

I'm beginner with PHP, would you know how to create a file from a POST request from NGINX ?

I'm beginner with PHP, would you know how to create a file from a POST request from NGINX ?

THAT is the question, and that question is NOT an Arduino question. There must be something like 2110 web sites dealing with PHP scripting.

GET requests are far easier to make and test. Why not start simple(r)? If you NEED the security that POST provides, you can switch to making, and handling, a POST request after you KNOW that the GET request (with non-super-top-secret-kill-after-reading data) works.

I think it is more an issue of protocol than an issue related to POST or GET request. I wonder if the the POST method of the library HTTPClient.h contains the same protocol than POST of PHP language.

Someone would have had this issue ?

there is only one HTTP protocol standard RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1

Little UP !

Has never someone already encountered this kind of issue with NGINX ?? Am I the first one ?

:confused:

it is no an issue. it is php coding and you are on Arduino forum. I never used php

I must recall that I use the ARDUINO IDE to program the ESP32 board.
If the issue comes from the HTTPClient.h library, there is a reason to give insight for all users using this library from ARDUINO IDE.

Thank you if someone else can bring me insight :slight_smile:

KevH:
I must recall that I use the ARDUINO IDE to program the ESP32 board.
If the issue comes from the HTTPClient.h library, there is a reason to give insight for all users using this library from ARDUINO IDE.

Thank you if someone else can bring me insight :slight_smile:

but your arduino code is ok. now you need to solve how to save the data to a file with php on server. and that is out of Arduino scope. you don't find php experts here

Thank for your answer. I gonna search for php.

For anyone who comes looking here for a solution.

The OPs issue was the header he was adding.

He needs to add:

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

Instead of:
http.addHeader("Content-Type", "text/plain");

If he had done this, his code would have worked fine.