| 1 |
= cowboy_req:set_resp_cookie(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:set_resp_cookie - Set a cookie |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
set_resp_cookie(Name, Value, Req :: cowboy_req:req()) |
| 12 |
-> set_resp_cookie(Name, Value, Req, #{}) |
| 13 |
|
| 14 |
set_resp_cookie(Name, Value, Req :: cowboy_req:req(), Opts) |
| 15 |
-> Req |
| 16 |
|
| 17 |
Name :: binary() %% case sensitive |
| 18 |
Value :: iodata() %% case sensitive |
| 19 |
Opts :: cow_cookie:cookie_opts() |
| 20 |
---- |
| 21 |
|
| 22 |
Set a cookie to be sent with the response. |
| 23 |
|
| 24 |
Note that cookie names are case sensitive. |
| 25 |
|
| 26 |
== Arguments |
| 27 |
|
| 28 |
Name:: |
| 29 |
|
| 30 |
Cookie name. |
| 31 |
|
| 32 |
Value:: |
| 33 |
|
| 34 |
Cookie value. |
| 35 |
|
| 36 |
Req:: |
| 37 |
|
| 38 |
The Req object. |
| 39 |
|
| 40 |
Opts:: |
| 41 |
|
| 42 |
Cookie options. |
| 43 |
|
| 44 |
== Return value |
| 45 |
|
| 46 |
A new Req object is returned. |
| 47 |
|
| 48 |
The returned Req object must be used from that point onward, |
| 49 |
otherwise the cookie will not be sent in the response. |
| 50 |
|
| 51 |
== Changelog |
| 52 |
|
| 53 |
* *2.0*: `set_resp_cookie/3` introduced as an alias to `set_resp_cookie/4` with no options. |
| 54 |
* *2.0*: The first argument type is now `binary()` instead of `iodata()`. |
| 55 |
* *1.0*: Function introduced. |
| 56 |
|
| 57 |
== Examples |
| 58 |
|
| 59 |
.Set a session cookie |
| 60 |
[source,erlang] |
| 61 |
---- |
| 62 |
SessionID = base64:encode(crypto:strong_rand_bytes(32)), |
| 63 |
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0). |
| 64 |
---- |
| 65 |
|
| 66 |
.Set a cookie with an expiration time |
| 67 |
[source,erlang] |
| 68 |
---- |
| 69 |
Req = cowboy_req:set_resp_cookie(<<"lang">>, <<"fr-FR">>, |
| 70 |
Req0, #{max_age => 3600}). |
| 71 |
---- |
| 72 |
|
| 73 |
.Delete a cookie |
| 74 |
[source,erlang] |
| 75 |
---- |
| 76 |
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, <<>>, |
| 77 |
Req0, #{max_age => 0}). |
| 78 |
---- |
| 79 |
|
| 80 |
.Set a cookie for a specific domain and path |
| 81 |
[source,erlang] |
| 82 |
---- |
| 83 |
Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>, |
| 84 |
Req0, #{domain => "my.example.org", path => "/account"}). |
| 85 |
---- |
| 86 |
|
| 87 |
.Restrict a cookie to HTTPS |
| 88 |
[source,erlang] |
| 89 |
---- |
| 90 |
SessionID = base64:encode(crypto:strong_rand_bytes(32)), |
| 91 |
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, |
| 92 |
Req0, #{secure => true}). |
| 93 |
---- |
| 94 |
|
| 95 |
.Restrict a cookie to HTTP |
| 96 |
[source,erlang] |
| 97 |
---- |
| 98 |
SessionID = base64:encode(crypto:strong_rand_bytes(32)), |
| 99 |
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, |
| 100 |
Req0, #{http_only => true}). |
| 101 |
---- |
| 102 |
|
| 103 |
== See also |
| 104 |
|
| 105 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 106 |
link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)], |
| 107 |
link:man:cowboy_req:set_resp_headers(3)[cowboy_req:set_resp_headers(3)], |
| 108 |
link:man:cowboy_req:reply(3)[cowboy_req:reply(3)], |
| 109 |
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)] |