summaryrefslogtreecommitdiffstats
path: root/docs/en/cowboy/2.3/guide/req/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/en/cowboy/2.3/guide/req/index.html')
-rw-r--r--docs/en/cowboy/2.3/guide/req/index.html60
1 files changed, 31 insertions, 29 deletions
diff --git a/docs/en/cowboy/2.3/guide/req/index.html b/docs/en/cowboy/2.3/guide/req/index.html
index f5beafd1..e66ffbd1 100644
--- a/docs/en/cowboy/2.3/guide/req/index.html
+++ b/docs/en/cowboy/2.3/guide/req/index.html
@@ -69,7 +69,7 @@
<p>The Req map contains a number of fields which are documented and can be accessed directly. They are the fields that have a direct mapping to HTTP: the request <code>method</code>; the HTTP <code>version</code> used; the effective URI components <code>scheme</code>, <code>host</code>, <code>port</code>, <code>path</code> and <code>qs</code>; the request <code>headers</code>; and the connection <code>peer</code> address and port.</p>
<p>Note that the <code>version</code> field can be used to determine whether a connection is using HTTP/2.</p>
<p>To access a field, you can simply match in the function head. The following example sends a simple &quot;Hello world!&quot; response when the <code>method</code> is GET, and a 405 error otherwise.</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -96,14 +96,14 @@ http://www.gnu.org/software/src-highlite -->
<p>All functions will crash if something goes wrong. There is usually no need to catch these errors, Cowboy will send the appropriate 4xx or 5xx response depending on where the crash occurred.</p>
<h2 id="_request_method">Request method</h2>
<p>The request method can be retrieved directly:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>#{<font color="#FF6600">method</font> <font color="#990000">:=</font> <font color="#009900">Method</font>} <font color="#990000">=</font> <font color="#009900">Req</font><font color="#990000">.</font></tt></pre>
</div></div>
<p>Or using a function:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -115,14 +115,14 @@ http://www.gnu.org/software/src-highlite -->
<p>There is typically no need to change behavior based on the HTTP version: Cowboy already does it for you.</p>
<p>It can be useful in some cases, though. For example, one may want to redirect HTTP/1.1 clients to use Websocket, while HTTP/2 clients keep using HTTP/2.</p>
<p>The HTTP version can be retrieved directly:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>#{<font color="#FF6600">version</font> <font color="#990000">:=</font> <font color="#009900">Version</font>} <font color="#990000">=</font> <font color="#009900">Req</font><font color="#990000">.</font></tt></pre>
</div></div>
<p>Or using a function:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -131,7 +131,7 @@ http://www.gnu.org/software/src-highlite -->
<p>Cowboy defines the <code>&apos;HTTP/1.0&apos;</code>, <code>&apos;HTTP/1.1&apos;</code> and <code>&apos;HTTP/2&apos;</code> versions. Custom protocols can define their own values as atoms.</p>
<h2 id="_effective_request_uri">Effective request URI</h2>
<p>The scheme, host, port, path and query string components of the effective request URI can all be retrieved directly:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -144,7 +144,7 @@ http://www.gnu.org/software/src-highlite -->
} <font color="#990000">=</font> <font color="#009900">Req</font><font color="#990000">.</font></tt></pre>
</div></div>
<p>Or using the related functions:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -158,7 +158,7 @@ http://www.gnu.org/software/src-highlite -->
<p>Cowboy defines only the <code>&lt;&lt;&quot;http&quot;&gt;&gt;</code> and <code>&lt;&lt;&quot;https&quot;&gt;&gt;</code> schemes. They are chosen so that the scheme will only be <code>&lt;&lt;&quot;https&quot;&gt;&gt;</code> for requests on secure HTTP/1.1 or HTTP/2 connections.</p>
<!-- @todo Is that tested well?-->
<p>The effective request URI itself can be reconstructed with the <code>cowboy_req:uri/1,2</code> function. By default, an absolute URI is returned:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -166,7 +166,7 @@ http://www.gnu.org/software/src-highlite -->
<font color="#009900">URI</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:uri</font></b>(<font color="#009900">Req</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>Options are available to either disable or replace some or all of the components. Various URIs or URI formats can be generated this way, including the origin form:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -174,7 +174,7 @@ http://www.gnu.org/software/src-highlite -->
<font color="#009900">URI</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:uri</font></b>(<font color="#009900">Req</font>, #{<font color="#0000FF">host</font> <font color="#990000">=&gt;</font> <font color="#000080">undefined</font>})<font color="#990000">.</font></tt></pre>
</div></div>
<p>The protocol relative form:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -182,14 +182,14 @@ http://www.gnu.org/software/src-highlite -->
<font color="#009900">URI</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:uri</font></b>(<font color="#009900">Req</font>, #{<font color="#0000FF">scheme</font> <font color="#990000">=&gt;</font> <font color="#000080">undefined</font>})<font color="#990000">.</font></tt></pre>
</div></div>
<p>The absolute URI without a query string:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">URI</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:uri</font></b>(<font color="#009900">Req</font>, #{<font color="#0000FF">qs</font> <font color="#990000">=&gt;</font> <font color="#000080">undefined</font>})<font color="#990000">.</font></tt></pre>
</div></div>
<p>A different host:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -200,21 +200,21 @@ http://www.gnu.org/software/src-highlite -->
<p>Bindings are the host and path components that you chose to extract when defining the routes of your application. They are only available after the routing.</p>
<p>Cowboy provides functions to retrieve one or all bindings.</p>
<p>To retrieve a single value:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">Value</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:binding</font></b>(<font color="#FF6600">userid</font>, <font color="#009900">Req</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>When attempting to retrieve a value that was not bound, <code>undefined</code> will be returned. A different default value can be provided:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">Value</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:binding</font></b>(<font color="#FF6600">userid</font>, <font color="#009900">Req</font>, <font color="#993399">42</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>To retrieve everything that was bound:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -223,14 +223,14 @@ http://www.gnu.org/software/src-highlite -->
<p>They are returned as a map, with keys being atoms.</p>
<p>The Cowboy router also allows you to capture many host or path segments at once using the <code>...</code> qualifier.</p>
<p>To retrieve the segments captured from the host name:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">HostInfo</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:host_info</font></b>(<font color="#009900">Req</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>And the path segments:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -239,7 +239,7 @@ http://www.gnu.org/software/src-highlite -->
<p>Cowboy will return <code>undefined</code> if <code>...</code> was not used in the route.</p>
<h2 id="_query_parameters">Query parameters</h2>
<p>Cowboy provides two functions to access query parameters. You can use the first to get the entire list of parameters.</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -251,21 +251,21 @@ http://www.gnu.org/software/src-highlite -->
<p>The same is true when trying to use the PHP-style suffix <code>[]</code>. When a query string is <code>key[]=1&amp;key[]=2</code>, the list returned will contain two parameters of name <code>key[]</code>.</p>
<p>When a query string is simply <code>key</code>, Cowboy will return the list <code>[{&lt;&lt;&quot;key&quot;&gt;&gt;, true}]</code>, using <code>true</code> to indicate that the parameter <code>key</code> was defined, but with no value.</p>
<p>The second function Cowboy provides allows you to match out only the parameters you are interested in, and at the same time do any post processing you require using <a href="../constraints">constraints</a>. This function returns a map.</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>#{<font color="#FF6600">id</font> <font color="#990000">:=</font> <font color="#009900">ID</font>, <font color="#FF6600">lang</font> <font color="#990000">:=</font> <font color="#009900">Lang</font>} <font color="#990000">=</font> <b><font color="#000000">cowboy_req:match_qs</font></b>([<font color="#FF6600">id</font>, <font color="#FF6600">lang</font>], <font color="#009900">Req</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>Constraints can be applied automatically. The following snippet will crash when the <code>id</code> parameter is not an integer, or when the <code>lang</code> parameter is empty. At the same time, the value for <code>id</code> will be converted to an integer term:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">QsMap</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:match_qs</font></b>([{<font color="#FF6600">id</font>, <font color="#FF6600">int</font>}, {<font color="#FF6600">lang</font>, <font color="#FF6600">nonempty</font>}], <font color="#009900">Req</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>A default value may also be provided. The default will be used if the <code>lang</code> key is not found. It will not be used if the key is found but has an empty value.</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -276,7 +276,7 @@ http://www.gnu.org/software/src-highlite -->
<h2 id="_headers">Headers</h2>
<p>Header values can be retrieved either as a binary string or parsed into a more meaningful representation.</p>
<p>The get the raw value:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -284,21 +284,21 @@ http://www.gnu.org/software/src-highlite -->
</div></div>
<p>Cowboy expects all header names to be provided as lowercase binary strings. This is true for both requests and responses, regardless of the underlying protocol.</p>
<p>When the header is missing from the request, <code>undefined</code> will be returned. A different default can be provided:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">HeaderVal</font> <font color="#990000">=</font> <b><font color="#000000">cowboy_req:header</font></b>(<font color="#990000">&lt;&lt;</font><font color="#FF0000">"content-type"</font><font color="#990000">&gt;&gt;</font>, <font color="#009900">Req</font>, <font color="#990000">&lt;&lt;</font><font color="#FF0000">"text/plain"</font><font color="#990000">&gt;&gt;</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>All headers can be retrieved at once, either directly:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>#{<font color="#FF6600">headers</font> <font color="#990000">:=</font> <font color="#009900">AllHeaders</font>} <font color="#990000">=</font> <font color="#009900">Req</font><font color="#990000">.</font></tt></pre>
</div></div>
<p>Or using a function:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -306,7 +306,7 @@ http://www.gnu.org/software/src-highlite -->
</div></div>
<p>Cowboy provides equivalent functions to parse individual headers. There is no function to parse all headers at once.</p>
<p>To parse a specific header:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -314,7 +314,7 @@ http://www.gnu.org/software/src-highlite -->
</div></div>
<p>An exception will be thrown if it doesn&apos;t know how to parse the given header, or if the value is invalid. The list of known headers and default values can be found in the manual.</p>
<p>When the header is missing, <code>undefined</code> is returned. You can change the default value. Note that it should be the parsed value directly:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -324,14 +324,14 @@ http://www.gnu.org/software/src-highlite -->
<h2 id="_peer">Peer</h2>
<p>The peer address and port number for the connection can be retrieved either directly or using a function.</p>
<p>To retrieve the peer directly:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>#{<font color="#FF6600">peer</font> <font color="#990000">:=</font> {<font color="#009900">IP</font>, <font color="#009900">Port</font>}} <font color="#990000">=</font> <font color="#009900">Req</font><font color="#990000">.</font></tt></pre>
</div></div>
<p>And using a function:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -395,6 +395,8 @@ http://www.gnu.org/software/src-highlite -->
+ <li><a href="/docs/en/cowboy/2.7/guide">2.7</a></li>
+
<li><a href="/docs/en/cowboy/2.6/guide">2.6</a></li>
<li><a href="/docs/en/cowboy/2.5/guide">2.5</a></li>