| 1 |
= cowboy_req:header(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:header - HTTP header |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
header(Name, Req) -> header(Name, Req, undefined) |
| 12 |
header(Name, Req, Default) -> binary() | Default |
| 13 |
|
| 14 |
Name :: binary() %% lowercase; case insensitive |
| 15 |
Req :: cowboy_req:req() |
| 16 |
Default :: any() |
| 17 |
---- |
| 18 |
|
| 19 |
Return the value for the given HTTP header. |
| 20 |
|
| 21 |
The header name must be given as a lowercase binary string. |
| 22 |
While header names are case insensitive, Cowboy requires them |
| 23 |
to be given as lowercase to function properly. |
| 24 |
|
| 25 |
Headers can also be obtained using pattern matching: |
| 26 |
|
| 27 |
[source,erlang] |
| 28 |
---- |
| 29 |
#{headers := #{Name := Value}} = Req. |
| 30 |
---- |
| 31 |
|
| 32 |
Note that this snippet will crash if the header is missing. |
| 33 |
|
| 34 |
== Arguments |
| 35 |
|
| 36 |
Name:: |
| 37 |
|
| 38 |
Desired HTTP header name as a lowercase binary string. |
| 39 |
|
| 40 |
Req:: |
| 41 |
|
| 42 |
The Req object. |
| 43 |
|
| 44 |
Default:: |
| 45 |
|
| 46 |
Default value returned when the header is missing. |
| 47 |
|
| 48 |
== Return value |
| 49 |
|
| 50 |
The header value is returned as a binary string. When the |
| 51 |
header is missing, the default argument is returned. |
| 52 |
|
| 53 |
== Changelog |
| 54 |
|
| 55 |
* *2.0*: Only the header value is returned, it is no longer wrapped in a tuple. |
| 56 |
* *1.0*: Function introduced. |
| 57 |
|
| 58 |
== Examples |
| 59 |
|
| 60 |
.Get the accept header |
| 61 |
[source,erlang] |
| 62 |
---- |
| 63 |
Accept = cowboy_req:header(<<"accept">>, Req). |
| 64 |
---- |
| 65 |
|
| 66 |
.Get the content-length header with a default value |
| 67 |
[source,erlang] |
| 68 |
---- |
| 69 |
Length = cowboy_req:header(<<"content-length">>, Req, <<"0">>). |
| 70 |
---- |
| 71 |
|
| 72 |
== See also |
| 73 |
|
| 74 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 75 |
link:man:cowboy_req:headers(3)[cowboy_req:headers(3)], |
| 76 |
link:man:cowboy_req:parse_header(3)[cowboy_req:parse_header(3)] |