| 1 |
= cowboy_req:scheme(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:scheme - URI scheme |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
scheme(Req :: cowboy_req:req()) -> Scheme :: binary() |
| 12 |
---- |
| 13 |
|
| 14 |
Return the scheme of the effective request URI. |
| 15 |
|
| 16 |
The scheme can also be obtained using pattern matching: |
| 17 |
|
| 18 |
[source,erlang] |
| 19 |
---- |
| 20 |
#{scheme := Scheme} = Req. |
| 21 |
---- |
| 22 |
|
| 23 |
== Arguments |
| 24 |
|
| 25 |
Req:: |
| 26 |
|
| 27 |
The Req object. |
| 28 |
|
| 29 |
== Return value |
| 30 |
|
| 31 |
The scheme is returned as a binary. It is case insensitive. |
| 32 |
|
| 33 |
Cowboy will only set the scheme to `<<"http">>` or `<<"https">>`. |
| 34 |
|
| 35 |
== Changelog |
| 36 |
|
| 37 |
* *2.0*: Function introduced. |
| 38 |
|
| 39 |
== Examples |
| 40 |
|
| 41 |
.Redirect HTTP to HTTPS |
| 42 |
[source,erlang] |
| 43 |
---- |
| 44 |
init(Req0=#{scheme := <<"http">>}, State) -> |
| 45 |
Req = cowboy_req:reply(302, #{ |
| 46 |
<<"location">> => cowboy_req:uri(Req, #{scheme => <<"https">>}) |
| 47 |
}, Req0), |
| 48 |
{ok, Req, State}; |
| 49 |
init(Req, State) -> |
| 50 |
{cowboy_rest, Req, State}. |
| 51 |
---- |
| 52 |
|
| 53 |
== See also |
| 54 |
|
| 55 |
link:man:cowboy_req(3)[cowboy_req(3)] |