ESPAsyncWebServer
ESPAsyncWebServer copied to clipboard
Respond with content using a callback containing templates -> How access to GET params in callback?
In this example:
String processor(const String& var) { if(var == "HELLO_FROM_TEMPLATE"){ String inputMessage = request->getParam("f")->value(); //HOW IS POSSIBLE? return F("Hello world!"+inputMessage ); } return String(); } request->send(SPIFFS, "/index.htm", String(), false, processor);
how is possibile retrive a param value in callback processot?
One possibility is to use a lambda function that binds the request and pass it to the processor function as an additional parameter:
String processor(const String& var, AsyncWebServerRequest* request) {
if (var == "HELLO_FROM_TEMPLATE") {
if (request->hasParam("f")) {
return String("hello world!") + request->getParam("f")->value();
}
}
return String("");
}
void handleRoot(AsyncWebServerRequest* request) {
request->send(SPIFFS, "/index.htm", String(), false, [request](const String& var) {
return processor(var, request);
});
}
void setupWebServer() {
server.on("/", HTTP_GET, handleRoot);
server.begin();
}