diff options
Diffstat (limited to 'docs/en/cowboy/2.0/guide/rest_handlers/index.html')
-rw-r--r-- | docs/en/cowboy/2.0/guide/rest_handlers/index.html | 409 |
1 files changed, 144 insertions, 265 deletions
diff --git a/docs/en/cowboy/2.0/guide/rest_handlers/index.html b/docs/en/cowboy/2.0/guide/rest_handlers/index.html index 7edacfa9..99d3b986 100644 --- a/docs/en/cowboy/2.0/guide/rest_handlers/index.html +++ b/docs/en/cowboy/2.0/guide/rest_handlers/index.html @@ -62,283 +62,162 @@ <h1 class="lined-header"><span>REST handlers</span></h1> -<div class="paragraph"><p>REST is implemented in Cowboy as a sub protocol. The request -is handled as a state machine with many optional callbacks -describing the resource and modifying the machine’s behavior.</p></div> -<div class="paragraph"><p>The REST handler is the recommended way to handle HTTP requests.</p></div> -<div class="sect1"> +<p>REST is implemented in Cowboy as a sub protocol. The request is handled as a state machine with many optional callbacks describing the resource and modifying the machine's behavior.</p> +<p>The REST handler is the recommended way to handle HTTP requests.</p> <h2 id="_initialization">Initialization</h2> -<div class="sectionbody"> -<div class="paragraph"><p>First, the <code>init/2</code> callback is called. This callback is common -to all handlers. To use REST for the current request, this function -must return a <code>cowboy_rest</code> tuple.</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight +<p>First, the <code>init/2</code> callback is called. This callback is common to all handlers. To use REST for the current request, this function must return a <code>cowboy_rest</code> tuple.</p> +<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8 by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -<pre><tt><span style="font-weight: bold"><span style="color: #000000">init</span></span>(<span style="color: #009900">Req</span>, <span style="color: #009900">State</span>) <span style="color: #990000">-></span> - {<span style="color: #FF6600">cowboy_rest</span>, <span style="color: #009900">Req</span>, <span style="color: #009900">State</span>}<span style="color: #990000">.</span></tt></pre></div></div> -<div class="paragraph"><p>Cowboy will then switch to the REST protocol and start executing -the state machine.</p></div> -<div class="paragraph"><p>After reaching the end of the flowchart, the <code>terminate/3</code> callback -will be called if it is defined.</p></div> -</div> -</div> -<div class="sect1"> +<pre><tt><b><font color="#000000">init</font></b>(<font color="#009900">Req</font>, <font color="#009900">State</font>) <font color="#990000">-></font> + {<font color="#FF6600">cowboy_rest</font>, <font color="#009900">Req</font>, <font color="#009900">State</font>}<font color="#990000">.</font></tt></pre> +</div></div> +<p>Cowboy will then switch to the REST protocol and start executing the state machine.</p> +<p>After reaching the end of the flowchart, the <code>terminate/3</code> callback will be called if it is defined.</p> <h2 id="_methods">Methods</h2> -<div class="sectionbody"> -<div class="paragraph"><p>The REST component has code for handling the following HTTP methods: -HEAD, GET, POST, PATCH, PUT, DELETE and OPTIONS.</p></div> -<div class="paragraph"><p>Other methods can be accepted, however they have no specific callback -defined for them at this time.</p></div> -</div> -</div> -<div class="sect1"> +<p>The REST component has code for handling the following HTTP methods: HEAD, GET, POST, PATCH, PUT, DELETE and OPTIONS.</p> +<p>Other methods can be accepted, however they have no specific callback defined for them at this time.</p> <h2 id="_callbacks">Callbacks</h2> -<div class="sectionbody"> -<div class="paragraph"><p>All callbacks are optional. Some may become mandatory depending -on what other defined callbacks return. The various flowcharts -in the next chapter should be a useful to determine which callbacks -you need.</p></div> -<div class="paragraph"><p>All callbacks take two arguments, the Req object and the State, -and return a three-element tuple of the form <code>{Value, Req, State}</code>.</p></div> -<div class="paragraph"><p>All callbacks can also return <code>{stop, Req, State}</code> to stop execution -of the request.</p></div> -<div class="paragraph"><p>The following table summarizes the callbacks and their default values. -If the callback isn’t defined, then the default value will be used. -Please look at the flowcharts to find out the result of each return -value.</p></div> -<div class="paragraph"><p>In the following table, "skip" means the callback is entirely skipped -if it is undefined, moving directly to the next step. Similarly, -"none" means there is no default value for this callback.</p></div> -<div class="tableblock"> -<table rules="all" -width="100%" -frame="border" -cellspacing="0" cellpadding="4"> -<col width="50%" /> -<col width="50%" /> -<thead> -<tr> -<th align="left" valign="top"> Callback name </th> -<th align="center" valign="top"> Default value</th> -</tr> -</thead> -<tbody> -<tr> -<td align="left" valign="top"><p class="table">allowed_methods</p></td> -<td align="center" valign="top"><p class="table"><code>[<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">allow_missing_post</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">charsets_provided</p></td> -<td align="center" valign="top"><p class="table">skip</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">content_types_accepted</p></td> -<td align="center" valign="top"><p class="table">none</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">content_types_provided</p></td> -<td align="center" valign="top"><p class="table"><code>[{{ <<"text">>, <<"html">>, '*'}, to_html}]</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">delete_completed</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">delete_resource</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">expires</p></td> -<td align="center" valign="top"><p class="table"><code>undefined</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">forbidden</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">generate_etag</p></td> -<td align="center" valign="top"><p class="table"><code>undefined</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">is_authorized</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">is_conflict</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">known_methods</p></td> -<td align="center" valign="top"><p class="table"><code>[<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">languages_provided</p></td> -<td align="center" valign="top"><p class="table">skip</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">last_modified</p></td> -<td align="center" valign="top"><p class="table"><code>undefined</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">malformed_request</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">moved_permanently</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">moved_temporarily</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">multiple_choices</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">options</p></td> -<td align="center" valign="top"><p class="table"><code>ok</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">previously_existed</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">resource_exists</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">service_available</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">uri_too_long</p></td> -<td align="center" valign="top"><p class="table"><code>false</code></p></td> +<p>All callbacks are optional. Some may become mandatory depending on what other defined callbacks return. The various flowcharts in the next chapter should be a useful to determine which callbacks you need.</p> +<p>All callbacks take two arguments, the Req object and the State, and return a three-element tuple of the form <code>{Value, Req, State}</code>.</p> +<p>All callbacks can also return <code>{stop, Req, State}</code> to stop execution of the request.</p> +<p>The following table summarizes the callbacks and their default values. If the callback isn't defined, then the default value will be used. Please look at the flowcharts to find out the result of each return value.</p> +<p>In the following table, "skip" means the callback is entirely skipped if it is undefined, moving directly to the next step. Similarly, "none" means there is no default value for this callback.</p> +<table rules="all" width="100%" frame="border" + cellspacing="0" cellpadding="4"> +<thead><tr><th>Callback name</th> +<th>Default value</th> +</tr></thead><tbody><tr><td>allowed_methods</td> +<td><code>[<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]</code></td> </tr> -<tr> -<td align="left" valign="top"><p class="table">valid_content_headers</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> +<tr><td>allow_missing_post</td> +<td><code>true</code></td> </tr> -<tr> -<td align="left" valign="top"><p class="table">valid_entity_length</p></td> -<td align="center" valign="top"><p class="table"><code>true</code></p></td> +<tr><td>charsets_provided</td> +<td>skip</td> </tr> -<tr> -<td align="left" valign="top"><p class="table">variances</p></td> -<td align="center" valign="top"><p class="table"><code>[]</code></p></td> +<tr><td>content_types_accepted</td> +<td>none</td> </tr> -</tbody> -</table> -</div> -<div class="paragraph"><p>As you can see, Cowboy tries to move on with the request whenever -possible by using well thought out default values.</p></div> -<div class="paragraph"><p>In addition to these, there can be any number of user-defined -callbacks that are specified through <code>content_types_accepted/2</code> -and <code>content_types_provided/2</code>. They can take any name, however -it is recommended to use a separate prefix for the callbacks of -each function. For example, <code>from_html</code> and <code>to_html</code> 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.</p></div> -</div> -</div> -<div class="sect1"> +<tr><td>content_types_provided</td> +<td><code>[{{ <<"text">>, <<"html">>, '*'}, to_html}]</code></td> +</tr> +<tr><td>delete_completed</td> +<td><code>true</code></td> +</tr> +<tr><td>delete_resource</td> +<td><code>false</code></td> +</tr> +<tr><td>expires</td> +<td><code>undefined</code></td> +</tr> +<tr><td>forbidden</td> +<td><code>false</code></td> +</tr> +<tr><td>generate_etag</td> +<td><code>undefined</code></td> +</tr> +<tr><td>is_authorized</td> +<td><code>true</code></td> +</tr> +<tr><td>is_conflict</td> +<td><code>false</code></td> +</tr> +<tr><td>known_methods</td> +<td><code>[<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]</code></td> +</tr> +<tr><td>languages_provided</td> +<td>skip</td> +</tr> +<tr><td>last_modified</td> +<td><code>undefined</code></td> +</tr> +<tr><td>malformed_request</td> +<td><code>false</code></td> +</tr> +<tr><td>moved_permanently</td> +<td><code>false</code></td> +</tr> +<tr><td>moved_temporarily</td> +<td><code>false</code></td> +</tr> +<tr><td>multiple_choices</td> +<td><code>false</code></td> +</tr> +<tr><td>options</td> +<td><code>ok</code></td> +</tr> +<tr><td>previously_existed</td> +<td><code>false</code></td> +</tr> +<tr><td>resource_exists</td> +<td><code>true</code></td> +</tr> +<tr><td>service_available</td> +<td><code>true</code></td> +</tr> +<tr><td>uri_too_long</td> +<td><code>false</code></td> +</tr> +<tr><td>valid_content_headers</td> +<td><code>true</code></td> +</tr> +<tr><td>valid_entity_length</td> +<td><code>true</code></td> +</tr> +<tr><td>variances</td> +<td><code>[]</code></td> +</tr> +</tbody></table> +<p>As you can see, Cowboy tries to move on with the request whenever possible by using well thought out default values.</p> +<p>In addition to these, there can be any number of user-defined callbacks that are specified through <code>content_types_accepted/2</code> and <code>content_types_provided/2</code>. They can take any name, however it is recommended to use a separate prefix for the callbacks of each function. For example, <code>from_html</code> and <code>to_html</code> 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.</p> <h2 id="_meta_data">Meta data</h2> -<div class="sectionbody"> -<div class="paragraph"><p>Cowboy will set informative values to the Req object at various -points of the execution. You can retrieve them by matching the -Req object directly. The values are defined in the following table:</p></div> -<div class="tableblock"> -<table rules="all" -width="100%" -frame="border" -cellspacing="0" cellpadding="4"> -<col width="50%" /> -<col width="50%" /> -<thead> -<tr> -<th align="left" valign="top"> Key </th> -<th align="left" valign="top"> Details</th> -</tr> -</thead> -<tbody> -<tr> -<td align="left" valign="top"><p class="table">media_type</p></td> -<td align="left" valign="top"><p class="table">The content-type negotiated for the response entity.</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">language</p></td> -<td align="left" valign="top"><p class="table">The language negotiated for the response entity.</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">charset</p></td> -<td align="left" valign="top"><p class="table">The charset negotiated for the response entity.</p></td> -</tr> -</tbody> -</table> -</div> -<div class="paragraph"><p>They can be used to send a proper body with the response to a -request that used a method other than HEAD or GET.</p></div> -</div> -</div> -<div class="sect1"> +<p>Cowboy will set informative values to the Req object at various points of the execution. You can retrieve them by matching the Req object directly. The values are defined in the following table:</p> +<table rules="all" width="100%" frame="border" + cellspacing="0" cellpadding="4"> +<thead><tr><th>Key</th> +<th>Details</th> +</tr></thead><tbody><tr><td>media_type</td> +<td>The content-type negotiated for the response entity.</td> +</tr> +<tr><td>language</td> +<td>The language negotiated for the response entity.</td> +</tr> +<tr><td>charset</td> +<td>The charset negotiated for the response entity.</td> +</tr> +</tbody></table> +<p>They can be used to send a proper body with the response to a request that used a method other than HEAD or GET.</p> <h2 id="_response_headers">Response headers</h2> -<div class="sectionbody"> -<div class="paragraph"><p>Cowboy will set response headers automatically over the execution -of the REST code. They are listed in the following table.</p></div> -<div class="tableblock"> -<table rules="all" -width="100%" -frame="border" -cellspacing="0" cellpadding="4"> -<col width="50%" /> -<col width="50%" /> -<thead> -<tr> -<th align="left" valign="top"> Header name </th> -<th align="left" valign="top"> Details</th> -</tr> -</thead> -<tbody> -<tr> -<td align="left" valign="top"><p class="table">content-language</p></td> -<td align="left" valign="top"><p class="table">Language used in the response body</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">content-type</p></td> -<td align="left" valign="top"><p class="table">Media type and charset of the response body</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">etag</p></td> -<td align="left" valign="top"><p class="table">Etag of the resource</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">expires</p></td> -<td align="left" valign="top"><p class="table">Expiration date of the resource</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">last-modified</p></td> -<td align="left" valign="top"><p class="table">Last modification date for the resource</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">location</p></td> -<td align="left" valign="top"><p class="table">Relative or absolute URI to the requested resource</p></td> -</tr> -<tr> -<td align="left" valign="top"><p class="table">vary</p></td> -<td align="left" valign="top"><p class="table">List of headers that may change the representation of the resource</p></td> -</tr> -</tbody> -</table> -</div> -</div> -</div> +<p>Cowboy will set response headers automatically over the execution of the REST code. They are listed in the following table.</p> +<table rules="all" width="100%" frame="border" + cellspacing="0" cellpadding="4"> +<thead><tr><th>Header name</th> +<th>Details</th> +</tr></thead><tbody><tr><td>content-language</td> +<td>Language used in the response body</td> +</tr> +<tr><td>content-type</td> +<td>Media type and charset of the response body</td> +</tr> +<tr><td>etag</td> +<td>Etag of the resource</td> +</tr> +<tr><td>expires</td> +<td>Expiration date of the resource</td> +</tr> +<tr><td>last-modified</td> +<td>Last modification date for the resource</td> +</tr> +<tr><td>location</td> +<td>Relative or absolute URI to the requested resource</td> +</tr> +<tr><td>vary</td> +<td>List of headers that may change the representation of the resource</td> +</tr> +</tbody></table> + |