| 1 |
= cowboy_req:parse_qs(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:parse_qs - Parse the query string |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
parse_qs(Req :: cowboy_req:req()) -> parse_qs(Req, #{}) |
| 12 |
parse_qs(Req :: cowboy_req:req(), Opts) |
| 13 |
-> [{Key :: binary(), Value :: binary() | true}] |
| 14 |
|
| 15 |
Opts :: #{max_keys => non_neg_integer()} |
| 16 |
---- |
| 17 |
|
| 18 |
Parse the query string as a list of key/value pairs. |
| 19 |
|
| 20 |
== Arguments |
| 21 |
|
| 22 |
Req:: |
| 23 |
|
| 24 |
The Req object. |
| 25 |
|
| 26 |
Opts:: |
| 27 |
|
| 28 |
The `max_keys` option limits the maximum number of |
| 29 |
keys in the query string. It defaults to 100. |
| 30 |
|
| 31 |
== Return value |
| 32 |
|
| 33 |
The parsed query string is returned as a list of key/value pairs. |
| 34 |
The key is a binary string. The value is either a binary string, |
| 35 |
or the atom `true`. Both key and value are case sensitive. |
| 36 |
|
| 37 |
The atom `true` is returned when a key is present in the query |
| 38 |
string without a value. For example, in the following URIs |
| 39 |
the key `<<"edit">>` will always have the value `true`: |
| 40 |
|
| 41 |
* `/posts/42?edit` |
| 42 |
* `/posts/42?edit&exclusive=1` |
| 43 |
* `/posts/42?exclusive=1&edit` |
| 44 |
* `/posts/42?exclusive=1&edit&from=web` |
| 45 |
|
| 46 |
== Changelog |
| 47 |
|
| 48 |
* *2.17*: Function `parse_qs/2` introduced with a `max_keys` option. |
| 49 |
* *2.0*: The parsed value is not longer cached in the Req object. |
| 50 |
* *2.0*: Only the parsed query string is returned, it is no longer wrapped in a tuple. |
| 51 |
* *2.0*: Function introduced. Replaces `qs_val/1` and `qs_vals/1`. |
| 52 |
|
| 53 |
== Examples |
| 54 |
|
| 55 |
.Parse the query string and convert the keys to atoms |
| 56 |
[source,erlang] |
| 57 |
---- |
| 58 |
ParsedQs = cowboy_req:parse_qs(Req), |
| 59 |
AtomsQs = [{binary_to_existing_atom(K, latin1), V} |
| 60 |
|| {K, V} <- ParsedQs]. |
| 61 |
---- |
| 62 |
|
| 63 |
== See also |
| 64 |
|
| 65 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 66 |
link:man:cowboy_req:qs(3)[cowboy_req:qs(3)], |
| 67 |
link:man:cowboy_req:match_qs(3)[cowboy_req:match_qs(3)] |