| 1 |
= cowboy_req:match_qs(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:match_qs - Match the query string against constraints |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
match_qs(Fields :: cowboy:fields(), Req :: cowboy_req:req()) |
| 12 |
-> match_qs(Fields, Req, #{}) |
| 13 |
match_qs(Fields :: cowboy:fields(), Req :: cowboy_req:req(), Opts) |
| 14 |
-> #{atom() => any()} |
| 15 |
|
| 16 |
Opts :: #{max_keys => non_neg_integer()} |
| 17 |
---- |
| 18 |
|
| 19 |
Parse the query string and match specific values against |
| 20 |
constraints. |
| 21 |
|
| 22 |
Cowboy will only return the query string values specified |
| 23 |
in the fields list, and ignore all others. Fields can be |
| 24 |
either the key requested; the key along with a list of |
| 25 |
constraints; or the key, a list of constraints and a |
| 26 |
default value in case the key is missing. |
| 27 |
|
| 28 |
This function will crash if the key is missing and no |
| 29 |
default value is provided. This function will also crash |
| 30 |
if a constraint fails. |
| 31 |
|
| 32 |
The key must be provided as an atom. The key of the |
| 33 |
returned map will be that atom. The value may be converted |
| 34 |
through the use of constraints, making this function able |
| 35 |
to extract, validate and convert values all in one step. |
| 36 |
|
| 37 |
== Arguments |
| 38 |
|
| 39 |
Fields:: |
| 40 |
|
| 41 |
Fields to retrieve from the query string. |
| 42 |
+ |
| 43 |
See link:man:cowboy(3)[cowboy(3)] for a complete description. |
| 44 |
|
| 45 |
Req:: |
| 46 |
|
| 47 |
The Req object. |
| 48 |
|
| 49 |
Opts:: |
| 50 |
|
| 51 |
The `max_keys` option limits the maximum number of |
| 52 |
keys in the query string. It defaults to 100. |
| 53 |
|
| 54 |
== Return value |
| 55 |
|
| 56 |
Desired values are returned as a map. The key is the atom |
| 57 |
that was given in the list of fields, and the value is the |
| 58 |
optionally converted value after applying constraints. |
| 59 |
|
| 60 |
The map contains the same keys that were given in the fields. |
| 61 |
|
| 62 |
An exception is triggered when the match fails. |
| 63 |
|
| 64 |
== Changelog |
| 65 |
|
| 66 |
* *2.17*: Function `match_qs/3` introduced with a `max_keys` option. |
| 67 |
* *2.0*: Function introduced. |
| 68 |
|
| 69 |
== Examples |
| 70 |
|
| 71 |
.Match fields |
| 72 |
[source,erlang] |
| 73 |
---- |
| 74 |
%% ID and Lang are binaries. |
| 75 |
#{id := ID, lang := Lang} |
| 76 |
= cowboy_req:match_qs([id, lang], Req). |
| 77 |
---- |
| 78 |
|
| 79 |
.Match fields and apply constraints |
| 80 |
[source,erlang] |
| 81 |
---- |
| 82 |
%% ID is an integer and Lang a non-empty binary. |
| 83 |
#{id := ID, lang := Lang} |
| 84 |
= cowboy_req:match_qs([{id, int}, {lang, nonempty}], Req). |
| 85 |
---- |
| 86 |
|
| 87 |
.Match fields with default values |
| 88 |
[source,erlang] |
| 89 |
---- |
| 90 |
#{lang := Lang} |
| 91 |
= cowboy_req:match_qs([{lang, [], <<"en-US">>}], Req). |
| 92 |
---- |
| 93 |
|
| 94 |
== See also |
| 95 |
|
| 96 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 97 |
link:man:cowboy_req:qs(3)[cowboy_req:qs(3)], |
| 98 |
link:man:cowboy_req:parse_qs(3)[cowboy_req:parse_qs(3)] |