| 1 |
= cowboy_req:binding(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:binding - Access a value bound from the route |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
binding(Name, Req) -> binding(Name, Req, undefined) |
| 12 |
binding(Name, Req, Default) -> any() | Default |
| 13 |
|
| 14 |
Name :: atom() |
| 15 |
Req :: cowboy_req:req() |
| 16 |
Default :: any() |
| 17 |
---- |
| 18 |
|
| 19 |
Return the value for the given binding. |
| 20 |
|
| 21 |
== Arguments |
| 22 |
|
| 23 |
Name:: |
| 24 |
|
| 25 |
Desired binding name as an atom. |
| 26 |
|
| 27 |
Req:: |
| 28 |
|
| 29 |
The Req object. |
| 30 |
|
| 31 |
Default:: |
| 32 |
|
| 33 |
Default value returned when the binding is missing. |
| 34 |
|
| 35 |
== Return value |
| 36 |
|
| 37 |
By default the value is a case sensitive binary string, however |
| 38 |
constraints may change the type of this value (for example |
| 39 |
automatically converting numbers to integer). |
| 40 |
|
| 41 |
== Changelog |
| 42 |
|
| 43 |
* *2.0*: Only the value is returned, it is no longer wrapped in a tuple. |
| 44 |
* *1.0*: Function introduced. |
| 45 |
|
| 46 |
== Examples |
| 47 |
|
| 48 |
.Get the username from the path |
| 49 |
[source,erlang] |
| 50 |
---- |
| 51 |
%% Route is "/users/:user" |
| 52 |
Username = cowboy_req:binding(user, Req). |
| 53 |
---- |
| 54 |
|
| 55 |
.Get the branch name, with a default |
| 56 |
[source,erlang] |
| 57 |
---- |
| 58 |
%% Route is "/log[/:branch]" |
| 59 |
Branch = cowboy_req:binding(branch, Req, <<"master">>) |
| 60 |
---- |
| 61 |
|
| 62 |
== See also |
| 63 |
|
| 64 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 65 |
link:man:cowboy_req:bindings(3)[cowboy_req:bindings(3)], |
| 66 |
link:man:cowboy_req:host_info(3)[cowboy_req:host_info(3)], |
| 67 |
link:man:cowboy_req:path_info(3)[cowboy_req:path_info(3)], |
| 68 |
link:man:cowboy_router(3)[cowboy_router(3)] |