gmag11 / FSBrowserNG

Full autocontained (on SPIFFS) async web server on ESP8266. Written as a Library.
Other
158 stars 70 forks source link

Trouble with REST callback #29

Closed GitBeagle closed 6 years ago

GitBeagle commented 7 years ago

I'm trying to build on the UserConfigExample using the REST callback. I have been able to create userConfig.json and userConfig.html files. When invoked the userConfig.html file displays the correct data from the json file. When I try to modify the data the url updates with the arguments showing the new data values. However the REST callback does not see these arguments. The callback always prints zero arguments.

I am a complete newB when it comes to web programming and not much better with C++. Given an example I can usually figure out how to tailor it to my needs. This example assumes greater web knowledge than I possess!

I have attempted to attach my userConfig.html file (as a text file) for reference.

Here's my callback code:

void  callbackREST(AsyncWebServerRequest *request)
{
  //its possible to test the url and do different things, 
  //test you rest URL
  if (request->url() == "/rest/userConfig")
  {
   printf("# args = %d\n", request->args());  // always returns zero arguments
   if (request->args() > 0)  // Save Settings
    {
        String data;
        Serial.println("Updating user configuration parameters");
        for (uint8_t i = 0; i < request->args(); i++) {
            if (request->argName(i) == "mqttUser") {
                data = ESPHTTPServer.urldecode(request->arg(i));
                printf("mqttUser = %s\n", data.c_str());
                ESPHTTPServer.save_user_config("mqttUser", data);
                continue;
            }
            if (request->argName(i) == "mqttPass") {
                data = ESPHTTPServer.urldecode(request->arg(i));
                printf("mqttPass = %s\n", data.c_str());
                ESPHTTPServer.save_user_config("mqttUser", data);
                continue;
            }
            /*if (request->argName(i) == "tz") {
                _config.timezone = request->arg(i).toInt();
                NTP.setTimeZone(_config.timezone / 10);
                continue;
            }
            if (request->argName(i) == "dst") {
                _config.daylight = true;
                DEBUGLOG("Daylight Saving: %d\r\n", _config.daylight);
                continue;
            }*/
        }
    }

    //contruct and send and desired repsonse
    // get sample data from json file
    String values = "mqttUser|"+ mqttUser +"|input\n";
    values += "mqttPass|"+ mqttPass +"|input\n";
    request->send(200, "text/plain", values);
    Serial.println(values);
    values = "";

  } else if (request->url() == "/rest/user") 
  {
    String data = "data1";
    //ESPHTTPServer.load_user_config("user1", data);
    String values = "user1|"+ data +"|input\n";

    //ESPHTTPServer.load_user_config("user2", data);
    data = "data2";
    values += "user2|" + data + "|input\n";

    //ESPHTTPServer.load_user_config("user3", data);
    data = "data3";
    values += "user3|" + data + "|input\n";
    request->send(200, "text/plain", values);
    Serial.println(values);
    values = "";
  }
  else 
  { 
    //its possible to test the url and do different things, 
    String values = "message:Hello world! \nurl:" + request->url() + "\n";
    request->send(200, "text/plain", values);
    values = "";
  }
}

userConfig.txt

species5618 commented 7 years ago

without looking at you whole code it is difficult to say but in the rest section i dont see where you initialise mqttPass & mqttUser

if these are global variables you either need to update the values in the post code or reload the config to ensure they have the new values in

my assumption should be easy to prove, do the correct values get saved in the json file. you can use the file browser or a reset to confirm this

GitBeagle commented 7 years ago

The variables are global and have valid values read from the userConfig.json file as confirmed by a debug printf. When selecting the Save button on the userConfig.html file the url in the browser address line shows proper arguments with the new values for mqttUser and mqttPass. However, the rest callback always reports zero arguments so the code to store the new values never executes. Somewhere between submitting the url with arguments and the rest callback the arguments seem to disappear. I know the rest callback is executed as I have all the debug messages enabled.

Here are the first 3 lines of code in the rest callback. I can see the output from the printf statement before and after selecting Save. It always shows zero arguments.

void callbackREST(AsyncWebServerRequest *request)
{
if (request->url() == "/rest/userConfig")
{
printf("# args = %d\n", request->args()); // always returns zero arguments

Thank you for taking a look...

John