-1

I'm trying to set up an ESP32 (DOIT ESP32 DEVKIT V1 in the Arduino IDE, with Espressif's WiFi libraries) with the following includes so far:

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <HTTPSServer.hpp>
#include <SSLCert.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>

Now, if I have a callback routine that's set up with

ResourceNode * nodeEchoPost = new ResourceNode("/cmd", "POST", &handleEcho);

how can I parse out the POST request string? On the serial console I get

[HTTPS:I] Request: POST /cmd

which is a debug output from the HTTPSRequest library itself. But I have no clue how to get that '/cmd' string for my own use! I've tried the example code of

byte buffer[256];
size_t s = req->readBytes(buffer, 256);

getting a buffer that only returns garbage, probably because all the request is in the POST statement and the body is empty.

Since all I need is the request-info

'/cmd?command1=foo&command2=bar&etc'

to chop up and use, well.. how do I get that out from the library? I've been googling around, and looking in the library header files, without much luck.

Also, is there any advantage to using the WiFiClientSecure library instead, since everything already goes over HTTPS? That's more of a side question, though if it makes it easier to grab the Request string that way, I'm totally fine with accepting that.

0

I think I found the answer, by looking through examples in the github repo of Hessel's HTTPS library.

In short, this seems to be it:

void handleEcho(HTTPRequest * req, HTTPResponse * res) {

Serial.print("in handleEcho. request is:");
Serial.println(req->getRequestString().c_str());

Output from serial console:

"in handleEcho. request is:/cmd?command1=foo&cmd2=bar"

Now I have a plain-text string that I can use plain string commands on to chop up in parts with strtok or similar. I don't mind the extra hassle of finding keywords in the string, I'm happy to just have gotten the damn thing.

Since I have all the data I want in that request, I'll just use req->readBytes as mentioned in examples to empty the buffer and discard all 'body' data.

And someone's gotta agree with me that adding the .c_str() to an already existing function just seems extra complicated. :/

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.