Added the comments for the param function because had problem to understand the pattern of lists in URL parameters

This commit is contained in:
Laurent Meyer 2015-02-22 11:56:40 +01:00
parent e08b9220e1
commit 4edc907fde

View File

@ -106,15 +106,27 @@ int main()
return crow::response{os.str()}; return crow::response{os.str()};
}); });
// Example of a request taking URL parameters
// If you want to activate all the functions just query
// {ip}:18080/params?foo='blabla'&pew=32&count[]=a&count[]=b
CROW_ROUTE(app, "/params") CROW_ROUTE(app, "/params")
([](const crow::request& req){ ([](const crow::request& req){
std::ostringstream os; std::ostringstream os;
// To get a simple string from the url params
// To see it in action /param?foo='blabla'
os << "Params: " << req.url_params << "\n\n"; os << "Params: " << req.url_params << "\n\n";
os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n"; os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
// To get a double from the request
// To see in action submit something like '/params?pew=42'
if(req.url_params.get("pew") != nullptr) { if(req.url_params.get("pew") != nullptr) {
double countD = boost::lexical_cast<double>(req.url_params.get("pew")); double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
os << "The value of 'pew' is " << countD << '\n'; os << "The value of 'pew' is " << countD << '\n';
} }
// To get a list from the request
// You have to submit something like '/params?count[]=a&count[]=b' to have a list with two values (a and b)
auto count = req.url_params.get_list("count"); auto count = req.url_params.get_list("count");
os << "The key 'count' contains " << count.size() << " value(s).\n"; os << "The key 'count' contains " << count.size() << " value(s).\n";
for(const auto& countVal : count) { for(const auto& countVal : count) {