-1

We are developing a Web Console and REST API for the NodeMCU V3 ESP8266, using ESPAsyncWebServer. We are facing some difficulties to serve dynamic pages using File Containing Templates! We're unable to replace REQ_HOST_ID with request->host() using Template Processor! The following code is working fine when a static host name is supplied.

// server->on("/", HTTP_GET, HomePage::index);

void HomePage::index(AsyncWebServerRequest *request) {
    Serial.println(request->host());
    auto processor = [](const String &var) -> String {
        if (var == "REQ_HOST_ID"){
        //  return F(request->host()); //FIXME
            return F("10.10.10.1"); 
        }
        return String();
    };
    AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/www/index.html", String(), false, processor);
    response->addHeader("Server", "Academia Web Server");
    request->send(response);
}

Edited:

Commented code causes a compile-time error and needs to be fixed to set the value of request->host(). request->host().c_str() causes the same error!

/Users/Chorke/Documents/Arduino/libraries/AcademiaCoreWeb/src/pages/HomePage.cpp: In lambda function:
/Users/Chorke/Documents/Arduino/libraries/AcademiaCoreWeb/src/pages/HomePage.cpp:16:22: error: 'request' is not captured
             return F(request->host()); //FIXME
                      ^
/Users/Chorke/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/cores/esp8266/WString.h:38:76: note: in definition of macro 'FPSTR'
 #define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
                                                                            ^
/Users/Chorke/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/cores/esp8266/WString.h:39:34: note: in expansion of macro 'PSTR'
 #define F(string_literal) (FPSTR(PSTR(string_literal)))
                                  ^
/Users/Chorke/Documents/Arduino/libraries/AcademiaCoreWeb/src/pages/HomePage.cpp:16:20: note: in expansion of macro 'F'
             return F(request->host()); //FIXME

Your help resolving this issue is highly appreciated.

3
  • Buggy in which way?
    – Sim Son
    Apr 4 '20 at 10:46
  • return F(request->host()); causes compile time error! Need to fix this and it belonging issues. It's suppose to be there is two issue 1. request not accessible inside lambda expression and 2. directly belongs to the data type conversion Apr 4 '20 at 11:33
  • 2
    A compiler error is not a bug... please include the error message
    – Sim Son
    Apr 4 '20 at 17:30
0

Fixed:

auto processor = [request](const String &var) -> String {
    if (var == "REQ_HOST_ID"){
        return request->host().c_str();
    }
    return String();
};

Two mistake causes compile time error:

  1. Needs to pass request to the Lambda expression
  2. Need not to wrap the dynamic value with F() macro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.