Search code examples
c++restc++11flaskcrow

Fetch non-int (string) url resource-id in crow


How to get a non-int resource-id in c++ crow. I cannot add a route CROW_ROUTE(app, "/uid/<std::string>") or CROW_ROUTE(app, "/uid/<char*>") as it fails to compile. The examples donot have such a case. I have tried

int main() {
    crow::SimpleApp app;
    CROW_ROUTE(app, "/uid/*").methods("GET"_method)
    ([](const crow::request& req){
        return "hello";
        });

    CROW_ROUTE(app, "/uid/<int>").methods("GET"_method)
    ([](const crow::request& req, int id){
        return std::to_string(id);
        });
    app.port(8888).run();
}

But neither of them ( though correctly ) intercept GET /uid/uid_123 HTTP/1.1 ( with resource being a string "uid_123" )

For below python code, I want to acheive it in c++ crow library

from flask import Flask
app = Flask(__name__)
@app.route("/uid/<path:path>")
def hello1(path):
    print ("it is path is ", path)
    return "user id is -" + path

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8888)

Is there any workaround for the above?


Solution

  • Originally answered by erow and jihoo on github

    CROW_ROUTE(app,"/uid/<path>")
        ([](std::string path){
         return path;
         });
    

    In addition to int and path following also work :uint, double and string