Search code examples
getwebserveresp8266

Get more than one GET argument on ESP8266 Asynch Webserver


I would like to build a tiny webserver application for my ESP8266.

If i send the GET request from my browser to the ESP8266 Server, i can only receive one argument. Here is the code for my sending procedure:

<form method="get" action="/get">      
<fieldset>      
    <legend>Select Pumps</legend>      
    <input type="checkbox" name="message" value="1">Pump 1<br>      
    <input type="checkbox" name="message" value="2">Pump 2<br>      
    <input type="checkbox" name="message" value="3">Pump 3<br>
    <input type="checkbox" name="message" value="4">Pump 4<br>    
    <input type="checkbox" name="message" value="5">Pump 5<br>    
    <input type="checkbox" name="message" value="6">Pump 6<br>    
    <input type="checkbox" name="message" value="7">Pump 7<br>    
    <input type="checkbox" name="message" value="8">Pump 8<br>    
    <br>      
    <input type="submit" value="Start waterring!" />      
</fieldset>      

When the form has been sent, the server should give me back all arguments of the get request but it returns only the first argument

Hello, GET: 3
/get?message=3&message=4

Here is the actual code for processing the request:

    // Send a GET request to <IP>/get?message=<message>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String message;
    if (request->hasParam(PARAM_MESSAGE)) {
        message = request->getParam(PARAM_MESSAGE)->value();
    } else {
        message = "No message sent";
    }
    request->send(200, "text/plain", "Hello, GET: " + message);
});

I'm quite a newbie in programming webservers on microcontrollers and would appreciate any help :)


Solution

  • If you are using this: https://github.com/me-no-dev/ESPAsyncWebServer#request-variables there is actually solution there. You don't have to use names but you can validate them if that's required. Check section: GET, POST and FILE parameters.

    int params = request->params();
    for(int i=0;i<params;i++){
        AsyncWebParameter* p = request->getParam(i);
         ...
    }