I started with the ESP32 WebServer FSBrowser example. It uses POST requests to upload new files to the device's SPIFFS.
I would like to add a new POST handler called /run which accepts some text as the payload of the request and then prints it to the serial port.
The POST request is sent using the following Javascript:
var xhr = new XMLHttpRequest();
xhr.open("POST", "/run", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(code);
My handleRun function looks like this
void handleRun() {
DBG_OUTPUT_PORT.println("handleRun called");
DBG_OUTPUT_PORT.println(server.uri());
if (server.uri() != "/run") {
return;
}
HTTPUpload& upload = server.upload();
DBG_OUTPUT_PORT.println("server.upload() finished ");
DBG_OUTPUT_PORT.println(upload.status);
server.send(200, "text/plain", "");
}
The output from the serial port suggests that upload.status
is not valid:
handleRun called
/run
server.upload() finished
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
But I do not understand why.
How do you get the payload out of a POST request using ESP32 WebServer?