Page 1 of 1

ESP32 http post example

Posted: Mon Nov 26, 2018 1:29 pm
by Pcborges
Hi, I am currently using http GET request in my aplication to send data to my MySQL database via PHP script.
I have one PHP script for every function I need like insert, delete, update and for individual tables.
It is a lot of PHP scripts.

Now I intend to port the whole thing to use a single PHP script that receives the SQL preformated command that is already in use by an Android app to read the same tables from the same database.

The problem is that this PHP script has been designed to receive data via POSTs and not GETs and I do not want to change the original design so the solution is to modify my ESP32 code to send POSTs instead of GETs.

The piece of PHP script that first receives the data is as follow:

Code: Select all

if( isset($_POST['query']) && isset($_POST['key']) ){... 
The variables QUERY and KEY are again used latter in the application.

I am looking for examples I can use as base to make this move.

Thanks in advance
Paulo Borges

Re: ESP32 http post example

Posted: Tue Nov 27, 2018 12:02 am
by chegewara

Code: Select all

   WiFiClientSecure http;  
    
   http.begin("https://example.com", root_ca);  //Specify destination for HTTP request
   http.addHeader("Content-Type", "application/x-www-form-urlencoded");             //Specify content-type header

   int httpResponseCode = http.POST("query1=12345&query2=134684&query3=1");   //Send the actual POST request

   if(httpResponseCode>0){
.....

Re: ESP32 http post example

Posted: Tue Nov 27, 2018 2:17 am
by ESP_Sprite
Pcborges wrote:
Mon Nov 26, 2018 1:29 pm
Now I intend to port the whole thing to use a single PHP script that receives the SQL preformated command that is already in use by an Android app to read the same tables from the same database.
I hope you don't intend on using this architecture for anything serious? Because it'll take only one script kiddy getting access to that URL and doing the proverbial "DROP TABLE *" to make you hope your backups are in order...

Re: ESP32 http post example

Posted: Wed Nov 28, 2018 11:46 am
by Pcborges
Dear ESP_Sprite, thanks for the tip.
I do understand the risks and are also implementing ways so the PHP script will require some form of authentication before accepting the request.
Thanks again
Paulo

Re: ESP32 http post example

Posted: Wed Nov 28, 2018 11:50 am
by Pcborges
Dear Che, can you please clarify what is ROOT_CA on the line below:

Code: Select all

http.begin("https://example.com", root_ca);  //Specify destination for HTTP request
Another question is:
I never used

Code: Select all

WiFiClientSecure http; 
Do I have to?
Where do I get directions on how to use it?

Many thanks
Paulo Borges