<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nine Nines Support: Cowboy Function Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Change them or set them up as you like -->
<meta name="description" content="">
<meta name="author" content="(Soft10) Pol Cámara">
<!-- Stylesheets -->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,400italic' rel='stylesheet' type='text/css'>
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/99s.css" rel="stylesheet">
<!-- <link href="js/google-code-prettify/prettify.css" rel="stylesheet"> -->
<link href="/css/sh99s.css" rel="stylesheet"/>
<!-- Enables html5 support on older browsers, other js is placed at the end of the page to speed up loading -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" href="/img/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/img/ico/apple-touch-icon-114.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/img/ico/apple-touch-icon-72.png">
<link rel="apple-touch-icon-precomposed" href="/img/ico/apple-touch-icon-57.png">
<link rel="alternate" href="/feeds/atom.xml" type="application/atom+xml" title="Nine Nines Atom Feed">
</head>
<body class="big_text docs">
<header id="page-head">
<div id="topbar" class="container">
<div class="row">
<div class="span2">
<h1 id="logo"><a href="/" title="99s">99s</a></h1>
</div>
<div class="span10">
<!-- Top navigation and social icons-->
<div id="side-header">
<nav>
<ul>
<li><a title="Erlang training" href="/training">Training</a></li>
<li><a title="Technical publications" href="/articles">Articles</a></li>
<li><a title="Our talks" href="/talks">Talks</a></li>
<li class="active"><a title="Our services" href="/support">Pricing & Sponsoring</a></li>
<li><a title="Community support" href="https://ninenines.eu/archives/extend/">Mailing Lists</a></li>
<li><a title="Contact us" href="mailto:[email protected]">Contact</a></li>
</ul>
</nav>
<ul id="social">
<li>
<a href="https://github.com/ninenines" title="Check our Github repositories"><img src="/img/ico_github.png" data-hover="/img/ico_github_alt.png" alt="Github"></a>
</li>
<li class="dropdown" id="twitter-links">
<a href="#twitter-links" class="dropdown-toggle" data-toggle="dropdown" title="Follow us on Twitter">
<img src="/img/ico_microblog.png" data-hover="/img/ico_microblog_alt.png" alt="Twitter">
</a>
<ul class="dropdown-menu">
<li><a title="Visit Loïc Hoguin's Twitter Account" href="http://twitter.com/lhoguin">@lhoguin</a></li>
<!-- <li class="divider"></li>
<li><a title="Visit our official Twitter account" href="#">@99s</a></li> -->
</ul>
</li>
<!-- <li>
<a href="/css/" title="Add us on Linkedin"><img src="/img/ico_linkedin.png" data-hover="img/ico_linkedin_alt.png" alt="Linkedin"></a>
</li> -->
</ul>
</div>
</div>
</div>
</div>
</header>
<div id="contents" class="two_col">
<div class="container">
<div class="row">
<div id="docs" class="span9 maincol">
<h1 class="lined-header"><span>cowboy_rest</span></h1>
<p>The <code>cowboy_rest</code> module implements REST semantics on top of the HTTP protocol.</p>
<p>This module cannot be described as a behaviour due to most of the callbacks it defines being optional. It has the same semantics as a behaviour otherwise.</p>
<p>The only mandatory callback is <code>init/3</code>, needed to perform the protocol upgrade.</p>
<h2 id="types">Types</h2>
<p>None.</p>
<h2 id="meta_values">Meta values</h2>
<h3 id="charset">charset</h3>
<p>Type: binary()</p>
<p>Negotiated charset.</p>
<p>This value may not be defined if no charset was negotiated.</p>
<h3 id="language">language</h3>
<p>Type: binary()</p>
<p>Negotiated language.</p>
<p>This value may not be defined if no language was negotiated.</p>
<h3 id="media_type">media_type</h3>
<p>Type: {binary(), binary(), '*' | [{binary(), binary()}]}</p>
<p>Negotiated media-type.</p>
<p>The media-type is the content-type, excluding the charset.</p>
<p>This value is always defined after the call to <code>content_types_provided/2</code>.</p>
<h2 id="callbacks">Callbacks</h2>
<h3 id="init">init({TransportName, ProtocolName}, Req, Opts) -> {upgrade, protocol, cowboy_rest} | {upgrade, protocol, cowboy_rest, Req, Opts}</h3>
<p>Types:</p>
<ul>
<li>TransportName = tcp | ssl | atom()</li>
<li>ProtocolName = http | atom()</li>
<li>Req = cowboy_req:req()</li>
<li>Opts = any()</li>
</ul>
<p>Upgrade the protocol to <code>cowboy_rest</code>.</p>
<p>This is the only mandatory callback.</p>
<h3 id="rest_init">rest_init(Req, Opts) -> {ok, Req, State}</h3>
<p>Types:</p>
<ul>
<li>Req = cowboy_req:req()</li>
<li>Opts = any()</li>
<li>State = any()</li>
</ul>
<p>Initialize the state for this request.</p>
<h3 id="rest_terminate">rest_terminate(Req, State) -> ok</h3>
<p>Types:</p>
<ul>
<li>Req = cowboy_req:req()</li>
<li>State = any()</li>
</ul>
<p>Perform any necessary cleanup of the state.</p>
<p>This callback should release any resource currently in use, clear any active timer and reset the process to its original state, as it might be reused for future requests sent on the same connection.</p>
<h3 id="callback">Callback(Req, State) -> {Value, Req, State} | {halt, Req, State}</h3>
<p>Types:</p>
<ul>
<li>Callback - one of the REST callbacks described below</li>
<li>Req = cowboy_req:req()</li>
<li>State = any()</li>
<li>Value - see the REST callbacks description below</li>
</ul>
<p>Please see the REST callbacks description below for details on the <code>Value</code> type, the default value if the callback is not defined, and more general information on when the callback is called and what its intended use is.</p>
<p>The <code>halt</code> tuple can be returned to stop REST processing. It is up to the resource code to send a reply before that, otherwise a <code>204 No Content</code> will be sent.</p>
<h2 id="rest_callbacks_description">REST callbacks description</h2>
<h3 id="allowed_methods">allowed_methods</h3>
<ul>
<li>Methods: all</li>
<li>Value type: [binary()]</li>
<li>Default value: [<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]</li>
</ul>
<p>Return the list of allowed methods.</p>
<p>Methods are case sensitive. Standard methods are always uppercase.</p>
<h3 id="allow_missing_post">allow_missing_post</h3>
<ul>
<li>Methods: POST</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether POST is allowed when the resource doesn't exist.</p>
<p>Returning <code>true</code> here means that a new resource will be created. The URL to the created resource should also be returned from the <code>AcceptResource</code> callback.</p>
<h3 id="charsets_provided">charsets_provided</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: [binary()]</li>
<li>Skip to the next step if undefined</li>
</ul>
<p>Return the list of charsets the resource provides.</p>
<p>The list must be ordered in order of preference.</p>
<p>If the accept-charset header was not sent, the first charset in the list will be selected. Otherwise Cowboy will select the most appropriate charset from the list.</p>
<p>The chosen charset will be set in the <code>Req</code> object as the meta value <code>charset</code>.</p>
<p>While charsets are case insensitive, this callback is expected to return them as lowercase binary.</p>
<h3 id="content_types_accepted">content_types_accepted</h3>
<ul>
<li>Methods: POST, PUT, PATCH</li>
<li>No default</li>
</ul>
<p>Types:</p>
<ul>
<li>Value = [{binary() | {Type, SubType, Params}, AcceptResource}]</li>
<li>Type = SubType = binary()</li>
<li>Params = '*' | [{binary(), binary()}]</li>
<li>AcceptResource = atom()</li>
</ul>
<p>Return the list of content-types the resource accepts.</p>
<p>The list must be ordered in order of preference.</p>
<p>Each content-type can be given either as a binary string or as a tuple containing the type, subtype and parameters.</p>
<p>Cowboy will select the most appropriate content-type from the list. If any parameter is acceptable, then the tuple form should be used with parameters set to <code>'*'</code>. If the parameters value is set to <code>[]</code> only content-type values with no parameters will be accepted. All parameter values are treated in a case sensitive manner except the <code>charset</code> parameter, if present, which is case insensitive.</p>
<p>This function will be called for POST, PUT and PATCH requests. It is entirely possible to define different callbacks for different methods if the handling of the request differs. Simply verify what the method is with <code>cowboy_req:method/1</code> and return a different list for each methods.</p>
<p>The <code>AcceptResource</code> value is the name of the callback that will be called if the content-type matches. It is defined as follow.</p>
<ul>
<li>Value type: true | {true, URL} | false</li>
<li>No default</li>
</ul>
<p>Process the request body.</p>
<p>This function should create or update the resource with the information contained in the request body. This information may be full or partial depending on the request method.</p>
<p>If the request body was processed successfully, <code>true</code> must be returned. If the request method is POST, <code>{true, URL}</code> may be returned instead, and Cowboy will redirect the client to the location of the newly created resource.</p>
<p>If a response body must be sent, the appropriate media-type, charset and language can be retrieved using the <code>cowboy_req:meta/{2,3}</code> functions. The respective keys are <code>media_type</code>, <code>charset</code> and <code>language</code>. The body can be set using <code>cowboy_req:set_resp_body/2</code>.</p>
<h3 id="content_types_provided">content_types_provided</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Default value: [{{<<"text">>, <<"html">>, '*'}, to_html}]</li>
</ul>
<p>Types:</p>
<ul>
<li>Value = [{binary() | {Type, SubType, Params}, ProvideResource}]</li>
<li>Type = SubType = binary()</li>
<li>Params = '*' | [{binary(), binary()}]</li>
<li>ProvideResource = atom()</li>
</ul>
<p>Return the list of content-types the resource provides.</p>
<p>The list must be ordered in order of preference.</p>
<p>Each content-type can be given either as a binary string or as a tuple containing the type, subtype and parameters.</p>
<p>Cowboy will select the most appropriate content-type from the list. If any parameter is acceptable, then the tuple form should be used with parameters set to <code>'*'</code>. If the parameters value is set to <code>[]</code> only content-type values with no parameters will be accepted. All parameter values are treated in a case sensitive manner except the <code>charset</code> parameter, if present, which is case insensitive.</p>
<p>The <code>ProvideResource</code> value is the name of the callback that will be called if the content-type matches. It will only be called when a representation of the resource needs to be returned. It is defined as follow.</p>
<ul>
<li>Methods: GET, HEAD</li>
<li>Value type: iodata() | {stream, Fun} | {stream, Len, Fun} | {chunked, ChunkedFun}</li>
<li>No default</li>
</ul>
<p>Return the response body.</p>
<p>The response body may be provided directly or through a fun. If a fun tuple is returned, the appropriate <code>set_resp_body_fun</code> function will be called. Please refer to the documentation for these functions for more information about the types.</p>
<p>The call to this callback happens a good time after the call to <code>content_types_provided/2</code>, when it is time to start rendering the response body.</p>
<h3 id="delete_completed">delete_completed</h3>
<ul>
<li>Methods: DELETE</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether the delete action has been completed.</p>
<p>This function should return <code>false</code> if there is no guarantee that the resource gets deleted immediately from the system, including from any internal cache.</p>
<p>When this function returns <code>false</code>, a <code>202 Accepted</code> response will be sent instead of a <code>200 OK</code> or <code>204 No Content</code>.</p>
<h3 id="delete_resource">delete_resource</h3>
<ul>
<li>Methods: DELETE</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Delete the resource.</p>
<p>The value returned indicates if the action was successful, regardless of whether the resource is immediately deleted from the system.</p>
<h3 id="expires">expires</h3>
<ul>
<li>Methods: GET, HEAD</li>
<li>Value type: calendar:datetime() | binary() | undefined</li>
<li>Default value: undefined</li>
</ul>
<p>Return the date of expiration of the resource.</p>
<p>This date will be sent as the value of the expires header.</p>
<h3 id="forbidden">forbidden</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Return whether access to the resource is forbidden.</p>
<p>A <code>403 Forbidden</code> response will be sent if this function returns <code>true</code>. This status code means that access is forbidden regardless of authentication, and that the request shouldn't be repeated.</p>
<h3 id="generate_etag">generate_etag</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: binary() | {weak | strong, binary()}</li>
<li>Default value: undefined</li>
</ul>
<p>Return the entity tag of the resource.</p>
<p>This value will be sent as the value of the etag header.</p>
<p>If a binary is returned, then the value will be parsed to the tuple form automatically. The value must be in the same format as the etag header, including quotes.</p>
<h3 id="is_authorized">is_authorized</h3>
<ul>
<li>Methods: all</li>
<li>Value type: true | {false, AuthHeader}</li>
<li>Default value: true</li>
</ul>
<p>Return whether the user is authorized to perform the action.</p>
<p>This function should be used to perform any necessary authentication of the user before attempting to perform any action on the resource.</p>
<p>If the authentication fails, the value returned will be sent as the value for the www-authenticate header in the <code>401 Unauthorized</code> response.</p>
<h3 id="is_conflict">is_conflict</h3>
<ul>
<li>Methods: PUT</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Return whether the put action results in a conflict.</p>
<p>A <code>409 Conflict</code> response will be sent if this function returns <code>true</code>.</p>
<h3 id="known_content_type">known_content_type</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether the content-type is known.</p>
<p>This function determines if the server understands the content-type, regardless of its use by the resource.</p>
<h3 id="known_methods">known_methods</h3>
<ul>
<li>Methods: all</li>
<li>Value type: [binary()]</li>
<li>Default value: [<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]</li>
</ul>
<p>Return the list of known methods.</p>
<p>The full list of methods known by the server should be returned, regardless of their use in the resource.</p>
<p>The default value lists the methods Cowboy knows and implement in <code>cowboy_rest</code>.</p>
<p>Methods are case sensitive. Standard methods are always uppercase.</p>
<h3 id="languages_provided">languages_provided</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: [binary()]</li>
<li>Skip to the next step if undefined</li>
</ul>
<p>Return the list of languages the resource provides.</p>
<p>The list must be ordered in order of preference.</p>
<p>If the accept-language header was not sent, the first language in the list will be selected. Otherwise Cowboy will select the most appropriate language from the list.</p>
<p>The chosen language will be set in the <code>Req</code> object as the meta value <code>language</code>.</p>
<p>While languages are case insensitive, this callback is expected to return them as lowercase binary.</p>
<h3 id="last_modified">last_modified</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: calendar:datetime()</li>
<li>Default value: undefined</li>
</ul>
<p>Return the date of last modification of the resource.</p>
<p>This date will be used to test against the if-modified-since and if-unmodified-since headers, and sent as the last-modified header in the response of GET and HEAD requests.</p>
<h3 id="malformed_request">malformed_request</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Return whether the request is malformed.</p>
<p>Cowboy has already performed all the necessary checks by the time this function is called, so few resources are expected to implement it.</p>
<p>The check is to be done on the request itself, not on the request body, which is processed later.</p>
<h3 id="moved_permanently">moved_permanently</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: {true, URL} | false</li>
<li>Default value: false</li>
</ul>
<p>Return whether the resource was permanently moved.</p>
<p>If it was, its new URL is also returned and sent in the location header in the response.</p>
<h3 id="moved_temporarily">moved_temporarily</h3>
<ul>
<li>Methods: GET, HEAD, POST, PATCH, DELETE</li>
<li>Value type: {true, URL} | false</li>
<li>Default value: false</li>
</ul>
<p>Return whether the resource was temporarily moved.</p>
<p>If it was, its new URL is also returned and sent in the location header in the response.</p>
<h3 id="multiple_choices">multiple_choices</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Return whether there are multiple representations of the resource.</p>
<p>This function should be used to inform the client if there are different representations of the resource, for example different content-type. If this function returns <code>true</code>, the response body should include information about these different representations using <code>cowboy_req:set_resp_body/2</code>. The content-type of the response should be the one previously negociated and that can be obtained by calling <code>cowboy_req:meta(media_type, Req)</code>.</p>
<h3 id="options">options</h3>
<ul>
<li>Methods: OPTIONS</li>
<li>Value type: ok</li>
<li>Default value: ok</li>
</ul>
<p>Handle a request for information.</p>
<p>The response should inform the client the communication options available for this resource.</p>
<p>By default, Cowboy will send a <code>200 OK</code> response with the allow header set.</p>
<h3 id="previously_existed">previously_existed</h3>
<ul>
<li>Methods: GET, HEAD, POST, PATCH, DELETE</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Return whether the resource existed previously.</p>
<h3 id="resource_exists">resource_exists</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether the resource exists.</p>
<p>If it exists, conditional headers will be tested before attempting to perform the action. Otherwise, Cowboy will check if the resource previously existed first.</p>
<h3 id="service_available">service_available</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether the service is available.</p>
<p>This function can be used to test that all relevant backend systems are up and able to handle requests.</p>
<p>A <code>503 Service Unavailable</code> response will be sent if this function returns <code>false</code>.</p>
<h3 id="uri_too_long">uri_too_long</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: false</li>
</ul>
<p>Return whether the requested URI is too long.</p>
<p>Cowboy has already performed all the necessary checks by the time this function is called, so few resources are expected to implement it.</p>
<p>A <code>414 Request-URI Too Long</code> response will be sent if this function returns <code>true</code>.</p>
<h3 id="valid_content_headers">valid_content_headers</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether the content-* headers are valid.</p>
<p>This also applies to the transfer-encoding header. This function must return <code>false</code> for any unknown content-* headers, or if the headers can't be understood. The function <code>cowboy_req:parse_header/2</code> can be used to quickly check the headers can be parsed.</p>
<p>A <code>501 Not Implemented</code> response will be sent if this function returns <code>false</code>.</p>
<h3 id="valid_entity_length">valid_entity_length</h3>
<ul>
<li>Methods: all</li>
<li>Value type: boolean()</li>
<li>Default value: true</li>
</ul>
<p>Return whether the request body length is within acceptable boundaries.</p>
<p>A <code>413 Request Entity Too Large</code> response will be sent if this function returns <code>false</code>.</p>
<h3 id="variances">variances</h3>
<ul>
<li>Methods: GET, HEAD, POST, PUT, PATCH, DELETE</li>
<li>Value type: [binary()]</li>
<li>Default value: []</li>
</ul>
<p>Return the list of headers that affect the representation of the resource.</p>
<p>These request headers return the same resource but with different parameters, like another language or a different content-type.</p>
<p>Cowboy will automatically add the accept, accept-language and accept-charset headers to the list if the respective functions were defined in the resource.</p>
<p>This operation is performed right before the <code>resource_exists/2</code> callback. All responses past that point will contain the vary header which holds this list.</p>
<!-- a.code -->
</div>
<div class="span3 sidecol">
<div class="input-append">
<form id="form-search" class="form-search" action="#">
<input id="input-search" type="text" placeholder="Function search" autocomplete="off" autofocus class="input-medium search-query span2">
<button type="submit" class="btn btn-success">Go</button>
</form>
</div>
<h3 id="docs-nav">Navigation</h3>
<h3>See also</h3><ul><li><a href="/docs/en/cowboy/1.0/guide/">User Guide</a></li><li><a href="/docs/en/cowboy/1.0/index.html">README</a></li></ul>
<h3>Version select</h3>
<ul>
<li><a href="/docs/en/cowboy/1.0/manual/"><strong>1.0</strong></a></li>
<li><a href="/docs/en/cowboy/HEAD/manual/"><strong>HEAD</strong></a></li>
</ul>
</div>
</div>
</div>
</div>
<footer>
<div class="container">
<div class="row">
<div class="span6">
<p id="scroll-top"><a href="#">↑ Scroll to top</a></p>
<nav>
<ul>
<li><a href="mailto:[email protected]" title="Contact us">Contact us</a></li><li><a href="https://github.com/ninenines/ninenines.github.io" title="Github repository">Contribute to this site</a></li>
</ul>
</nav>
</div>
<div class="span6 credits">
<p><img src="/img/footer_logo.png"></p>
<p>Copyright © Nine Nines 2012-2014</p>
</div>
</div>
</div>
</footer>
<!-- Javascript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/js/bootstrap-carousel.js"></script>
<script src="/js/bootstrap-dropdown.js"></script>
<script src="/js/custom.js"></script>
<script type="text/javascript" src="/js/shCore.js"></script>
<script type="text/javascript" src="/js/shlang/shBrushBash.js"></script>
<script type="text/javascript" src="/js/shlang/shBrushErlang.js"></script>
<script type="text/javascript" src="/js/shlang/shBrushJScript.js"></script>
<script type="text/javascript" src="/js/shlang/shBrushPlain.js"></script>
<script type="text/javascript">SyntaxHighlighter.all();</script>
<script type="text/javascript" src="/js/fuse.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var f;
$.getJSON("/docs/db.json", function(data){
f = new Fuse(data, {keys: ["n"], threshold: 0.3});
$("<ul id=\"search-results\">").insertAfter("#form-search");
});
$("#input-search").keyup(function(e){if(f){if (e.which != 13 ){
var results = f.search($(this).val());
if (results.length == 0){
$("#form-search").attr("action", "#");
}else{
$("#form-search").attr("action", results[0].l);
}
$("#search-results").empty();
for (var i = 0; i < 10 && i < results.length; i++){
$("<li><a href=\"" + results[i].l + "\">" + results[i].n + "</a></li>")
.appendTo("#search-results");
}
}}});
});
</script>
</body>
</html>