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