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

Who is online

Users browsing this forum: Google [Bot] and 78 guests