ESPAsyncWebServer icon indicating copy to clipboard operation
ESPAsyncWebServer copied to clipboard

Respond with content using a callback containing templates -> How access to GET params in callback?

Open alessioburgassi opened this issue 2 years ago • 1 comments

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?

alessioburgassi avatar Dec 07 '23 22:12 alessioburgassi

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();
}

sivar2311 avatar Jan 07 '24 15:01 sivar2311