From 8459bebceb9533948193774371cbd9fd571b78ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 16 Oct 2019 09:48:31 +0200 Subject: Cowboy 2.7.0 --- docs/en/cowboy/2.5/guide/req/index.html | 60 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'docs/en/cowboy/2.5/guide/req') diff --git a/docs/en/cowboy/2.5/guide/req/index.html b/docs/en/cowboy/2.5/guide/req/index.html index bfa6156e..3db834c5 100644 --- a/docs/en/cowboy/2.5/guide/req/index.html +++ b/docs/en/cowboy/2.5/guide/req/index.html @@ -69,7 +69,7 @@

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 method; the HTTP version used; the effective URI components scheme, host, port, path and qs; the request headers; the connection peer address and port; and the TLS certificate cert when applicable.

Note that the version field can be used to determine whether a connection is using HTTP/2.

To access a field, you can simply match in the function head. The following example sends a simple "Hello world!" response when the method is GET, and a 405 error otherwise.

-
@@ -95,14 +95,14 @@ http://www.gnu.org/software/src-highlite -->

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.

Request method

The request method can be retrieved directly:

-
#{method := Method} = Req.

Or using a function:

-
@@ -114,14 +114,14 @@ http://www.gnu.org/software/src-highlite -->

There is typically no need to change behavior based on the HTTP version: Cowboy already does it for you.

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.

The HTTP version can be retrieved directly:

-
#{version := Version} = Req.

Or using a function:

-
@@ -130,7 +130,7 @@ http://www.gnu.org/software/src-highlite -->

Cowboy defines the 'HTTP/1.0', 'HTTP/1.1' and 'HTTP/2' versions. Custom protocols can define their own values as atoms.

Effective request URI

The scheme, host, port, path and query string components of the effective request URI can all be retrieved directly:

-
@@ -143,7 +143,7 @@ http://www.gnu.org/software/src-highlite --> } = Req.

Or using the related functions:

-
@@ -157,7 +157,7 @@ http://www.gnu.org/software/src-highlite -->

Cowboy defines only the <<"http">> and <<"https">> schemes. They are chosen so that the scheme will only be <<"https">> for requests on secure HTTP/1.1 or HTTP/2 connections.

The effective request URI itself can be reconstructed with the cowboy_req:uri/1,2 function. By default, an absolute URI is returned:

-
@@ -165,7 +165,7 @@ http://www.gnu.org/software/src-highlite --> URI = cowboy_req:uri(Req).

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:

-
@@ -173,7 +173,7 @@ http://www.gnu.org/software/src-highlite --> URI = cowboy_req:uri(Req, #{host => undefined}).

The protocol relative form:

-
@@ -181,14 +181,14 @@ http://www.gnu.org/software/src-highlite --> URI = cowboy_req:uri(Req, #{scheme => undefined}).

The absolute URI without a query string:

-
URI = cowboy_req:uri(Req, #{qs => undefined}).

A different host:

-
@@ -199,21 +199,21 @@ http://www.gnu.org/software/src-highlite -->

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.

Cowboy provides functions to retrieve one or all bindings.

To retrieve a single value:

-
Value = cowboy_req:binding(userid, Req).

When attempting to retrieve a value that was not bound, undefined will be returned. A different default value can be provided:

-
Value = cowboy_req:binding(userid, Req, 42).

To retrieve everything that was bound:

-
@@ -222,14 +222,14 @@ http://www.gnu.org/software/src-highlite -->

They are returned as a map, with keys being atoms.

The Cowboy router also allows you to capture many host or path segments at once using the ... qualifier.

To retrieve the segments captured from the host name:

-
HostInfo = cowboy_req:host_info(Req).

And the path segments:

-
@@ -238,7 +238,7 @@ http://www.gnu.org/software/src-highlite -->

Cowboy will return undefined if ... was not used in the route.

Query parameters

Cowboy provides two functions to access query parameters. You can use the first to get the entire list of parameters.

-
@@ -250,21 +250,21 @@ http://www.gnu.org/software/src-highlite -->

The same is true when trying to use the PHP-style suffix []. When a query string is key[]=1&key[]=2, the list returned will contain two parameters of name key[].

When a query string is simply key, Cowboy will return the list [{<<"key">>, true}], using true to indicate that the parameter key was defined, but with no value.

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 constraints. This function returns a map.

-
#{id := ID, lang := Lang} = cowboy_req:match_qs([id, lang], Req).

Constraints can be applied automatically. The following snippet will crash when the id parameter is not an integer, or when the lang parameter is empty. At the same time, the value for id will be converted to an integer term:

-
QsMap = cowboy_req:match_qs([{id, int}, {lang, nonempty}], Req).

A default value may also be provided. The default will be used if the lang key is not found. It will not be used if the key is found but has an empty value.

-
@@ -275,7 +275,7 @@ http://www.gnu.org/software/src-highlite -->

Headers

Header values can be retrieved either as a binary string or parsed into a more meaningful representation.

The get the raw value:

-
@@ -283,21 +283,21 @@ http://www.gnu.org/software/src-highlite -->

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.

When the header is missing from the request, undefined will be returned. A different default can be provided:

-
HeaderVal = cowboy_req:header(<<"content-type">>, Req, <<"text/plain">>).

All headers can be retrieved at once, either directly:

-
#{headers := AllHeaders} = Req.

Or using a function:

-
@@ -305,7 +305,7 @@ http://www.gnu.org/software/src-highlite -->

Cowboy provides equivalent functions to parse individual headers. There is no function to parse all headers at once.

To parse a specific header:

-
@@ -313,7 +313,7 @@ http://www.gnu.org/software/src-highlite -->

An exception will be thrown if it doesn'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.

When the header is missing, undefined is returned. You can change the default value. Note that it should be the parsed value directly:

-
@@ -323,14 +323,14 @@ http://www.gnu.org/software/src-highlite -->

Peer

The peer address and port number for the connection can be retrieved either directly or using a function.

To retrieve the peer directly:

-
#{peer := {IP, Port}} = Req.

And using a function:

-
@@ -394,6 +394,8 @@ http://www.gnu.org/software/src-highlite --> +
  • 2.7
  • +
  • 2.6
  • 2.5
  • -- cgit v1.2.3