2016-01-14 13:35:25 +01:00
|
|
|
[[rest_handlers]]
|
|
|
|
== REST handlers
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2014-09-30 20:12:13 +03:00
|
|
|
REST is implemented in Cowboy as a sub protocol. The request
|
|
|
|
is handled as a state machine with many optional callbacks
|
2013-01-01 18:27:41 +01:00
|
|
|
describing the resource and modifying the machine's behavior.
|
|
|
|
|
2014-09-30 20:12:13 +03:00
|
|
|
The REST handler is the recommended way to handle HTTP requests.
|
2013-02-11 09:26:13 +01:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Initialization
|
2013-04-26 14:12:29 +02:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
First, the `init/2` callback is called. This callback is common
|
2014-06-25 11:23:58 +02:00
|
|
|
to all handlers. To use REST for the current request, this function
|
2014-09-30 20:12:13 +03:00
|
|
|
must return a `cowboy_rest` tuple.
|
2013-04-26 14:12:29 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
2014-10-08 16:49:18 +02:00
|
|
|
init(Req, _Opts) ->
|
|
|
|
{cowboy_rest, Req, #state{}}.
|
2016-01-14 13:35:25 +01:00
|
|
|
----
|
2013-04-26 14:12:29 +02:00
|
|
|
|
|
|
|
Cowboy will then switch to the REST protocol and start executing
|
2014-09-26 15:58:44 +03:00
|
|
|
the state machine.
|
|
|
|
|
|
|
|
After reaching the end of the flowchart, the `terminate/3` callback
|
|
|
|
will be called if it is defined.
|
2013-04-26 14:12:29 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Methods
|
2013-04-19 13:54:43 +02:00
|
|
|
|
|
|
|
The REST component has code for handling the following HTTP methods:
|
|
|
|
HEAD, GET, POST, PATCH, PUT, DELETE and OPTIONS.
|
|
|
|
|
|
|
|
Other methods can be accepted, however they have no specific callback
|
|
|
|
defined for them at this time.
|
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Callbacks
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2013-04-11 21:52:09 +02:00
|
|
|
All callbacks are optional. Some may become mandatory depending
|
2014-06-21 18:50:50 +02:00
|
|
|
on what other defined callbacks return. The various flowcharts
|
|
|
|
in the next chapter should be a useful to determine which callbacks
|
|
|
|
you need.
|
2013-04-11 21:52:09 +02:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
All callbacks take two arguments, the Req object and the State,
|
|
|
|
and return a three-element tuple of the form `{Value, Req, State}`.
|
2013-04-11 21:52:09 +02:00
|
|
|
|
2014-11-07 20:19:05 +02:00
|
|
|
All callbacks can also return `{stop, Req, State}` to stop execution
|
2014-09-26 15:58:44 +03:00
|
|
|
of the request.
|
2013-04-11 21:52:09 +02:00
|
|
|
|
|
|
|
The following table summarizes the callbacks and their default values.
|
|
|
|
If the callback isn't defined, then the default value will be used.
|
2014-06-21 18:50:50 +02:00
|
|
|
Please look at the flowcharts to find out the result of each return
|
2013-04-11 21:52:09 +02:00
|
|
|
value.
|
|
|
|
|
|
|
|
In the following table, "skip" means the callback is entirely skipped
|
2014-07-06 13:10:35 +02:00
|
|
|
if it is undefined, moving directly to the next step. Similarly,
|
|
|
|
"none" means there is no default value for this callback.
|
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[cols="<,^",options="header"]
|
|
|
|
|===
|
|
|
|
| Callback name | Default value
|
|
|
|
| allowed_methods | `[<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]`
|
|
|
|
| allow_missing_post | `true`
|
|
|
|
| charsets_provided | skip
|
|
|
|
| content_types_accepted | none
|
|
|
|
| content_types_provided | `$$[{{<<"text">>, <<"html">>, '*'}, to_html}]$$`
|
|
|
|
| delete_completed | `true`
|
|
|
|
| delete_resource | `false`
|
|
|
|
| expires | `undefined`
|
|
|
|
| forbidden | `false`
|
|
|
|
| generate_etag | `undefined`
|
|
|
|
| is_authorized | `true`
|
|
|
|
| is_conflict | `false`
|
|
|
|
| known_methods | `[<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]`
|
|
|
|
| languages_provided | skip
|
|
|
|
| last_modified | `undefined`
|
|
|
|
| malformed_request | `false`
|
|
|
|
| moved_permanently | `false`
|
|
|
|
| moved_temporarily | `false`
|
|
|
|
| multiple_choices | `false`
|
|
|
|
| options | `ok`
|
|
|
|
| previously_existed | `false`
|
|
|
|
| resource_exists | `true`
|
|
|
|
| service_available | `true`
|
|
|
|
| uri_too_long | `false`
|
|
|
|
| valid_content_headers | `true`
|
|
|
|
| valid_entity_length | `true`
|
|
|
|
| variances | `[]`
|
|
|
|
|===
|
2013-04-11 21:52:09 +02:00
|
|
|
|
|
|
|
As you can see, Cowboy tries to move on with the request whenever
|
|
|
|
possible by using well thought out default values.
|
|
|
|
|
|
|
|
In addition to these, there can be any number of user-defined
|
|
|
|
callbacks that are specified through `content_types_accepted/2`
|
|
|
|
and `content_types_provided/2`. They can take any name, however
|
|
|
|
it is recommended to use a separate prefix for the callbacks of
|
|
|
|
each function. For example, `from_html` and `to_html` indicate
|
|
|
|
in the first case that we're accepting a resource given as HTML,
|
|
|
|
and in the second case that we send one as HTML.
|
2013-01-01 18:27:41 +01:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Meta data
|
2013-04-26 14:12:29 +02:00
|
|
|
|
2013-04-25 17:46:40 +02:00
|
|
|
Cowboy will set informative meta values at various points of the
|
|
|
|
execution. You can retrieve them using `cowboy_req:meta/{2,3}`.
|
|
|
|
The values are defined in the following table.
|
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[cols="<,<",options="header"]
|
|
|
|
|===
|
|
|
|
| Meta key | Details
|
|
|
|
| media_type | The content-type negotiated for the response entity.
|
|
|
|
| language | The language negotiated for the response entity.
|
|
|
|
| charset | The charset negotiated for the response entity.
|
|
|
|
|===
|
2013-04-25 17:46:40 +02:00
|
|
|
|
2013-08-27 18:32:53 +02:00
|
|
|
They can be used to send a proper body with the response to a
|
|
|
|
request that used a method other than HEAD or GET.
|
2013-04-25 17:46:40 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Response headers
|
2013-04-26 14:12:29 +02:00
|
|
|
|
|
|
|
Cowboy will set response headers automatically over the execution
|
|
|
|
of the REST code. They are listed in the following table.
|
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[cols="<,<",options="header"]
|
|
|
|
|===
|
|
|
|
| Header name | Details
|
|
|
|
| content-language | Language used in the response body
|
|
|
|
| content-type | Media type and charset of the response body
|
|
|
|
| etag | Etag of the resource
|
|
|
|
| expires | Expiration date of the resource
|
|
|
|
| last-modified | Last modification date for the resource
|
|
|
|
| location | Relative or absolute URI to the requested resource
|
|
|
|
| vary | List of headers that may change the representation of the resource
|
|
|
|
|===
|