I'm quite new to C++ and can't get my head around the documentation for Crow Web server.
What I want is to mock an RPC-endpoint, allowing query-parameters to be empty, and if so default them to "". The idea of using .get(value, defaultStr) seems missing or gone..
I'm getting an exception even asking for a parameter that's not there, and no option to set a default value from what i see in the docs.
(tagging with flask since Crow seems to be based upon it)
Here's what i've got:
CROW_ROUTE(api, "/rpc")
.methods(crow::HTTPMethod::Get)([&rpcParser](const crow::request &req) {
// Extract query parameters
std::string method = req.url_params.get("method");
std::string key = req.url_params.get("key");
std::string value = req.url_params.get("value");
// Check if any of the parameters are missing
if (method.empty()) {
return crow::response(400, "Missing required query parameters");
}
if (key.empty())
key = "";
if (value.empty())
value = "";
// Split the method at the first dot
size_t dot_pos = method.find('.');
if (dot_pos != std::string::npos) {
std::string class_name = method.substr(0, dot_pos); // Extract the part before the dot
std::string method_name = method.substr(dot_pos + 1); // Extract the part after the dot
// Call the RPC parser with the split values
rpcParser.parse(class_name, method_name, key, value);
} else {
// Handle the error when there is no dot in the method
// For example, you might want to send an error response
return crow::response(400, "Invalid method format");
}
// Return a response
return crow::response(200, "Request processed successfully");
});
Note to self and others:
.get() returns a char* , not std:string
easy way out (may be prettier ways):
std::string method = req.url_params.get("method") ? req.url_params.get("method") : "";
std::string key = req.url_params.get("key") ? req.url_params.get("key") : "";
std::string value = req.url_params.get("value") ? req.url_params.get("value") : "";