2014-07-06 13:10:35 +02:00
|
|
|
::: The Req object
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
The Req object is this variable that you will use to obtain
|
|
|
|
information about a request, read the body of the request
|
|
|
|
and send a response.
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: A special variable
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
While we call it an "object", it is not an object in the
|
|
|
|
OOP sense of the term. In fact it is completely opaque
|
|
|
|
to you and the only way you can perform operations using
|
|
|
|
it is by calling the functions from the `cowboy_req`
|
|
|
|
module.
|
|
|
|
|
|
|
|
Almost all the calls to the `cowboy_req` module will
|
|
|
|
return an updated request object. Just like you would
|
|
|
|
keep the updated `State` variable in a gen_server,
|
|
|
|
you MUST keep the updated `Req` variable in a Cowboy
|
|
|
|
handler. Cowboy will use this object to know whether
|
|
|
|
a response has been sent when the handler has finished
|
|
|
|
executing.
|
|
|
|
|
|
|
|
The Req object allows accessing both immutable and
|
|
|
|
mutable state. This means that calling some of the
|
|
|
|
functions twice will not produce the same result.
|
|
|
|
For example, when streaming the request body, the
|
|
|
|
function will return the body by chunks, one at a
|
|
|
|
time, until there is none left.
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Overview of the cowboy_req interface
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
With the exception of functions manipulating the request
|
|
|
|
body, all functions return a single value. Depending on
|
|
|
|
the function this can be the requested value (method,
|
|
|
|
host, path, ...), a boolean (has_body, has_resp_header...)
|
|
|
|
a new Req object (set_resp_body, set_resp_header...), or
|
|
|
|
simply the atom `ok` (chunk, continue, ...).
|
|
|
|
|
|
|
|
The request body reading functions may return `{Result, Req}`
|
|
|
|
or `{Result, Value, Req}`. The functions in this category
|
|
|
|
are `body/{1,2}`, `body_qs/{1,2}`, `part/{1,2}`, `part_body/{1,2}`.
|
|
|
|
|
|
|
|
This chapter covers the access functions mainly. Cookies,
|
|
|
|
request body and response functions are covered in their
|
|
|
|
own chapters.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Request
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
When a client performs a request, it first sends a few required
|
|
|
|
values. They are sent differently depending on the protocol
|
|
|
|
being used, but the intent is the same. They indicate to the
|
|
|
|
server the type of action it wants to do and how to locate
|
|
|
|
the resource to perform it on.
|
2013-01-18 18:04:21 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
The method identifies the action. Standard methods include
|
|
|
|
GET, HEAD, OPTIONS, PATCH, POST, PUT, DELETE. Method names
|
|
|
|
are case sensitive.
|
2013-01-18 18:04:21 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
Method = cowboy_req:method(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
The host, port and path parts of the URL identify the resource
|
|
|
|
being accessed. The host and port information may not be
|
|
|
|
available if the client uses HTTP/1.0.
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
Host = cowboy_req:host(Req),
|
|
|
|
Port = cowboy_req:port(Req),
|
|
|
|
Path = cowboy_req:path(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
The version used by the client can of course also be obtained.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
Version = cowboy_req:version(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Do note however that clients claiming to implement one version
|
|
|
|
of the protocol does not mean they implement it fully, or even
|
|
|
|
properly.
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Bindings
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
After routing the request, bindings are available. Bindings
|
|
|
|
are these parts of the host or path that you chose to extract
|
|
|
|
when defining the routes of your application.
|
|
|
|
|
|
|
|
You can fetch a single binding. The value will be `undefined`
|
|
|
|
if the binding doesn't exist.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
Binding = cowboy_req:binding(my_binding, Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
If you need a different value when the binding doesn't exist,
|
|
|
|
you can change the default.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
Binding = cowboy_req:binding(my_binding, Req, 42).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
You can also obtain all bindings in one call. They will be
|
|
|
|
returned as a list of key/value tuples.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
AllBindings = cowboy_req:bindings(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
If you used `...` at the beginning of the route's pattern
|
|
|
|
for the host, you can retrieve the matched part of the host.
|
|
|
|
The value will be `undefined` otherwise.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
HostInfo = cowboy_req:host_info(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Similarly, if you used `...` at the end of the route's
|
|
|
|
pattern for the path, you can retrieve the matched part,
|
|
|
|
or get `undefined` otherwise.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
PathInfo = cowboy_req:path_info(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Query string
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
The raw query string can be obtained directly.
|
|
|
|
|
|
|
|
``` erlang
|
|
|
|
Qs = cowboy_req:qs(Req).
|
|
|
|
```
|
|
|
|
|
|
|
|
You can parse the query string and then use standard library
|
|
|
|
functions to access individual values.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
QsVals = cowboy_req:parse_qs(Req),
|
|
|
|
{_, Lang} = lists:keyfind(<<"lang">>, 1, QsVals).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
You can match the query string into a map.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
``` erlang
|
2014-10-04 13:21:16 +03:00
|
|
|
#{id := ID, lang := Lang} = cowboy_req:match_qs([id, lang], Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
You can use constraints to validate the values while matching
|
|
|
|
them. The following snippet will crash if the `id` value is
|
|
|
|
not an integer number or if the `lang` value is empty. Additionally
|
|
|
|
the `id` value will be converted to an integer term, saving
|
|
|
|
you a conversion step.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
``` erlang
|
2014-10-04 13:21:16 +03:00
|
|
|
QsMap = cowboy_req:match_qs([{id, int}, {lang, nonempty}], Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
Note that in the case of duplicate query string keys, the map
|
|
|
|
value will become a list of the different values.
|
|
|
|
|
|
|
|
Read more about ^constraints^.
|
|
|
|
|
|
|
|
A default value can be provided. The default will be used
|
|
|
|
if the `lang` key is not found. It will not be used if
|
|
|
|
the key is found but has an empty value.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
``` erlang
|
2014-10-04 13:21:16 +03:00
|
|
|
#{lang := Lang} = cowboy_req:match_qs([{lang, [], <<"en-US">>}], Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
If no default is provided and the value is missing, the
|
|
|
|
query string is deemed invalid and the process will crash.
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Request URL
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
You can reconstruct the full URL of the resource.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
URL = cowboy_req:url(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
You can also obtain only the base of the URL, excluding the
|
|
|
|
path and query string.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
BaseURL = cowboy_req:host_url(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Headers
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
Cowboy allows you to obtain the header values as string,
|
|
|
|
or parsed into a more meaningful representation.
|
|
|
|
|
|
|
|
This will get the string value of a header.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
HeaderVal = cowboy_req:header(<<"content-type">>, Req).
|
2013-01-20 15:09:54 +01:00
|
|
|
```
|
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
You can of course set a default in case the header is missing.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
HeaderVal
|
2013-09-23 15:44:10 +02:00
|
|
|
= cowboy_req:header(<<"content-type">>, Req, <<"text/plain">>).
|
|
|
|
```
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
And also obtain all headers.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
AllHeaders = cowboy_req:headers(Req).
|
2013-01-20 15:09:54 +01:00
|
|
|
```
|
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
To parse the previous header, simply call `parse_header/{2,3}`
|
2014-09-23 16:43:29 +03:00
|
|
|
where you would call `header/{2,3}` otherwise.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
ParsedVal = cowboy_req:parse_header(<<"content-type">>, Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
2014-09-23 16:43:29 +03:00
|
|
|
Cowboy will crash if it doesn't know how to parse the given
|
|
|
|
header, or if the value is invalid.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
You can of course define a default value. Note that the default
|
|
|
|
value you specify here is the parsed value you'd like to get
|
|
|
|
by default.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
ParsedVal = cowboy_req:parse_header(<<"content-type">>, Req,
|
|
|
|
{<<"text">>, <<"plain">>, []}).
|
2013-01-20 15:09:54 +01:00
|
|
|
```
|
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
The list of known headers and default values is defined in the
|
2014-09-23 16:43:29 +03:00
|
|
|
manual.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Meta
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
Cowboy will sometimes associate some meta information with
|
|
|
|
the request. Built-in meta values are listed in the manual
|
|
|
|
for their respective modules.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
This will get a meta value. The returned value will be `undefined`
|
|
|
|
if it isn't defined.
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
MetaVal = cowboy_req:meta(websocket_version, Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
You can change the default value if needed.
|
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
MetaVal = cowboy_req:meta(websocket_version, Req, 13).
|
2013-09-23 15:44:10 +02:00
|
|
|
```
|
2013-01-20 15:09:54 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
You can also define your own meta values. The name must be
|
|
|
|
an `atom()`.
|
2013-01-29 13:32:48 +01:00
|
|
|
|
2013-09-23 15:44:10 +02:00
|
|
|
``` erlang
|
|
|
|
Req2 = cowboy_req:set_meta(the_answer, 42, Req).
|
|
|
|
```
|
|
|
|
|
2014-07-06 13:10:35 +02:00
|
|
|
:: Peer
|
2013-09-23 15:44:10 +02:00
|
|
|
|
|
|
|
You can obtain the peer address and port number. This is
|
|
|
|
not necessarily the actual IP and port of the client, but
|
|
|
|
rather the one of the machine that connected to the server.
|
2013-01-29 13:32:48 +01:00
|
|
|
|
|
|
|
``` erlang
|
2014-09-23 16:43:29 +03:00
|
|
|
{IP, Port} = cowboy_req:peer(Req).
|
2013-01-29 13:32:48 +01:00
|
|
|
```
|