-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from CrowCpp/quality_of_life
Quality of life improvements
- Loading branch information
Showing
8 changed files
with
379 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
A query string is the part of the url that comes after a `?` character, it is usually formatted as `key=value&otherkey=othervalue`. | ||
<br><br> | ||
|
||
Crow supports query strings through `crow::request::url_params`. The object is of type `crow::query_string` and can has the following functions:<br> | ||
##get(name) | ||
Returns the value (as char*) based on the given key (or name). Returns `nullptr` if the key is not found. | ||
##pop(name) | ||
Works the same as `get`, but removes the returned value. | ||
!!! note | ||
|
||
`crow::request::url_params` is a const value, therefore for pop (also pop_list and pop_dict) to work, a copy needs to be made. | ||
|
||
##get_list(name) | ||
A url can be `http://example.com?key[]=value1&key[]=value2&key[]=value3`. Using `get_list("key")` on such a url returns an `std::vector<std::string>` containing `[value1, value2, value3]`.<br><br> | ||
|
||
`#!cpp get_list("key", false)` can be used to parse `http://example.com?key=value1&key=value2&key=value3` | ||
##pop_list(name) | ||
Works the same as `get_list` but removes all instances of values having the given key (`use_brackets` is also available here). | ||
##get_dict(name) | ||
Returns an `std::unordered_map<std::string, std::string>` from a query string such as `?key[sub_key1]=value1&key[sub_key2]=value2&key[sub_key3]=value3`.<br> | ||
The key in the map is what's in the brackets (`sub_key1` for example), and the value being what's after the `=` sign (`value1`). The name passed to the function is not part of the returned value. | ||
##pop_dict(name) | ||
Works the same as `get_dict` but removing the values from the query string. | ||
!!!warning | ||
|
||
if your query string contains both a list and dictionary with the same key, it is best to use `pop_list` before either `get_dict` or `pop_dict`, since a map cannot contain more than one value per key, each item in the list will override the previous and only the last will remain with an empty key. | ||
|
||
<br><br> | ||
For more information take a look [here](../../reference/classcrow_1_1query__string.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#define CROW_MAIN | ||
#define CROW_JSON_USE_MAP | ||
#include "crow.h" | ||
|
||
|
||
|
||
int main() | ||
{ | ||
crow::SimpleApp app; | ||
|
||
// simple json response using a map | ||
// To see it in action enter {ip}:18080/json | ||
// it shoud show amessage before zmessage despite adding zmessage first. | ||
CROW_ROUTE(app, "/json") | ||
([]{ | ||
crow::json::wvalue x; | ||
x["zmessage"] = "Hello, World!"; | ||
x["amessage"] = "Hello, World2!"; | ||
return x; | ||
}); | ||
|
||
// enables all log | ||
app.loglevel(crow::LogLevel::Debug); | ||
|
||
app.port(18080) | ||
.multithreaded() | ||
.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.