Debian Codegraph @

doc/src/guide/rest_handlers.asciidoc

raw
1 [[rest_handlers]]
2 == REST handlers
3
4 REST is implemented in Cowboy as a sub protocol. The request
5 is handled as a state machine with many optional callbacks
6 describing the resource and modifying the machine's behavior.
7
8 The REST handler is the recommended way to handle HTTP requests.
9
10 === Initialization
11
12 First, the `init/2` callback is called. This callback is common
13 to all handlers. To use REST for the current request, this function
14 must return a `cowboy_rest` tuple.
15
16 [source,erlang]
17 ----
18 init(Req, State) ->
19 {cowboy_rest, Req, State}.
20 ----
21
22 Cowboy will then switch to the REST protocol and start executing
23 the state machine.
24
25 After reaching the end of the flowchart, the `terminate/3` callback
26 will be called if it is defined.
27
28 === Methods
29
30 The REST component has code for handling the following HTTP methods:
31 HEAD, GET, POST, PATCH, PUT, DELETE and OPTIONS.
32
33 Other methods can be accepted, however they have no specific callback
34 defined for them at this time.
35
36 === Callbacks
37
38 All callbacks are optional. Some may become mandatory depending
39 on what other defined callbacks return. The various flowcharts
40 in the next chapter should be a useful to determine which callbacks
41 you need.
42
43 All callbacks take two arguments, the Req object and the State,
44 and return a three-element tuple of the form `{Value, Req, State}`.
45
46 Nearly all callbacks can also return `{stop, Req, State}` to
47 stop execution of the request, and
48 `{{switch_handler, Module}, Req, State}` or
49 `{{switch_handler, Module, Opts}, Req, State}` to switch to
50 a different handler type. The exceptions are `expires`
51 `generate_etag`, `last_modified` and `variances`.
52
53 The following table summarizes the callbacks and their default values.
54 If the callback isn't defined, then the default value will be used.
55 Please look at the flowcharts to find out the result of each return
56 value.
57
58 In the following table, "skip" means the callback is entirely skipped
59 if it is undefined, moving directly to the next step. Similarly,
60 "none" means there is no default value for this callback.
61
62 [cols="<,^",options="header"]
63 |===
64 | Callback name | Default value
65 | allowed_methods | `[<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]`
66 | allow_missing_post | `true`
67 | charsets_provided | skip
68 | content_types_accepted | none
69 // @todo Space required for the time being: https://github.com/spf13/hugo/issues/2398
70 | content_types_provided | `[{{ <<"text">>, <<"html">>, '*'}, to_html}]`
71 | delete_completed | `true`
72 | delete_resource | `false`
73 | expires | `undefined`
74 | forbidden | `false`
75 | generate_etag | `undefined`
76 | is_authorized | `true`
77 | is_conflict | `false`
78 | known_methods | `[<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]`
79 | languages_provided | skip
80 | last_modified | `undefined`
81 | malformed_request | `false`
82 | moved_permanently | `false`
83 | moved_temporarily | `false`
84 | multiple_choices | `false`
85 | options | `ok`
86 | previously_existed | `false`
87 | ranges_provided | skip
88 | range_satisfiable | `true`
89 | rate_limited | `false`
90 | resource_exists | `true`
91 | service_available | `true`
92 | uri_too_long | `false`
93 | valid_content_headers | `true`
94 | valid_entity_length | `true`
95 | variances | `[]`
96 |===
97
98 As you can see, Cowboy tries to move on with the request whenever
99 possible by using well thought out default values.
100
101 In addition to these, there can be any number of user-defined
102 callbacks that are specified through `content_types_accepted/2`,
103 `content_types_provided/2` or `ranges_provided/2`. They can take
104 any name (except `auto` for range callbacks), however
105 it is recommended to use a separate prefix for the callbacks of
106 each function. For example, `from_html` and `to_html` indicate
107 in the first case that we're accepting a resource given as HTML,
108 and in the second case that we send one as HTML.
109
110 === Meta data
111
112 Cowboy will set informative values to the Req object at various
113 points of the execution. You can retrieve them by matching the
114 Req object directly. The values are defined in the following table:
115
116 [cols="<,<",options="header"]
117 |===
118 | Key | Details
119 | media_type | The content-type negotiated for the response entity
120 | language | The language negotiated for the response entity
121 | charset | The charset negotiated for the response entity
122 | range | The range selected for the ranged response
123 |===
124
125 They can be used to send a proper body with the response to a
126 request that used a method other than HEAD or GET.
127
128 === Response headers
129
130 Cowboy will set response headers automatically over the execution
131 of the REST code. They are listed in the following table.
132
133 [cols="<,<",options="header"]
134 |===
135 | Header name | Details
136 | accept-ranges | Range units accepted by the resource
137 | allow | HTTP methods allowed by the resource
138 | content-language | Language used in the response body
139 | content-range | Range of the content found in the response
140 | content-type | Media type and charset of the response body
141 | etag | Etag of the resource
142 | expires | Expiration date of the resource
143 | last-modified | Last modification date for the resource
144 | location | Relative or absolute URI to the requested resource
145 | retry-after | Delay or time the client should wait before accessing the resource
146 | vary | List of headers that may change the representation of the resource
147 | www-authenticate | Authentication information to access the resource
148 |===