Get data from HTTP_POST

Borisw37
Posts: 2
Joined: Sun Aug 05, 2018 6:00 pm

Get data from HTTP_POST

Postby Borisw37 » Thu Aug 30, 2018 12:36 am

Hello,

I'm writing a server application using the AsyncTCP.h and ESPAsyncWebServer.h librarires.

I'm able to receive POST requests and execute a function.

Code: Select all

	server.on("/LED", HTTP_POST, [](AsyncWebServerRequest *request){
		request->send(200, "text/plain", "Post route");
		handleLED();		
	});
How do I handle receiving data from the client?
For example:

Code: Select all

192.168.1.6/LED?key=val&key2=val2

boarchuz
Posts: 396
Joined: Tue Aug 21, 2018 5:28 am

Re: Get data from HTTP_POST

Postby boarchuz » Fri Aug 31, 2018 3:44 pm

Is the example code not working as expected?
Something like this should do:

Code: Select all

const char* PARAM_KEY = "key";
String keyVal;
if (request->hasParam(PARAM_KEY)) {
	keyVal= request->getParam(PARAM_KEY)->value();
} else {
	//handle an incomplete request
	keyVal = "some default value";
}
Also this

Code: Select all

192.168.1.6/LED?key=val&key2=val2
looks to me like you're using GET, not POST. A POST request would have the url "192.168.1.6/LED" with the postdata, "key=val&key2=val2", in the body of the request.

You may have to use HTTP_GET (or HTTP_ANY) instead of HTTP_POST so it's looking in the right place for your data.

billyjhnson
Posts: 2
Joined: Thu May 20, 2021 9:02 am

Re: Get data from HTTP_POST

Postby billyjhnson » Sun Aug 29, 2021 12:11 pm

Borisw37 wrote:
Thu Aug 30, 2018 12:36 am
Hello,

I'm writing a server application using the AsyncTCP.h and ESPAsyncWebServer.h librarires.

I'm able to receive POST requests and execute a function.

Code: Select all

	server.on("/LED", HTTP_POST, [](AsyncWebServerRequest *request){
		request->send(200, "text/plain", "Post route");
		handleLED();		
	});
How do I handle receiving data from the client?
For example:

Code: Select all

192.168.1.6/LED?key=val&key2=val2
In an HTTP POST request, the parameters are not sent along with the URI." - though it can be (just theoretically), do not confuse other people. POST, in accordance to spec, MUST serve non-idempotent requests, but you can use request body (which is segregated from Headers by ONE empty line), as well as request parameters.
billyjhnson

Check gbwhatsapp

GeorgeDaniels
Posts: 1
Joined: Fri Sep 10, 2021 10:17 am

Re: Get data from HTTP_POST

Postby GeorgeDaniels » Fri Sep 10, 2021 10:19 am

billyjhnson wrote:
Sun Aug 29, 2021 12:11 pm
Borisw37 wrote:
Thu Aug 30, 2018 12:36 am
Hello,

I'm writing a server application using the AsyncTCP.h and ESPAsyncWebServer.h librarires.

I'm able to receive POST requests and execute a function.

Code: Select all

	server.on("/LED", HTTP_POST, [](AsyncWebServerRequest *request){
		request->send(200, "text/plain", "Post route");
		handleLED();		
	});
How do I handle receiving data from the client?
For example:

Code: Select all

192.168.1.6/LED?key=val&key2=val2
In an HTTP POST request, the parameters are not sent along with the URI." - though it can be (just theoretically), do not confuse other people. POST, in accordance to spec, MUST serve non-idempotent requests, but you can use request body (which is segregated from Headers by ONE empty line), as well as request parameters.
Thanks a lot Billyjhnson for your wonderful answer. You are right, HTTP Post requests cant be sent along with URLs. You have a good day Sir.
KineMaster Mods

Regards: George_Daniels

Who is online

Users browsing this forum: Google [Bot], stevem and 42 guests