ESP32 http post example

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

ESP32 http post example

Postby Pcborges » Mon Nov 26, 2018 1:29 pm

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

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 http post example

Postby chegewara » Tue Nov 27, 2018 12:02 am

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){
.....

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 http post example

Postby ESP_Sprite » Tue Nov 27, 2018 2:17 am

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...

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

Re: ESP32 http post example

Postby Pcborges » Wed Nov 28, 2018 11:46 am

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

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

Re: ESP32 http post example

Postby Pcborges » Wed Nov 28, 2018 11:50 am

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

Who is online

Users browsing this forum: Baidu [Spider] and 53 guests