cowboy_req(3)

Name

cowboy_req - HTTP request and response

Description

The module cowboy_req provides functions to access, manipulate and respond to requests.

There are four types of functions in this module. They can be differentiated by their name and their return type:

Type Name pattern Return type
access no verb, parse_*, match_* Value
question has_* boolean()
modification set_* Req
action any other verb ok | {Result, Value, Req}

Any Req returned must be used in place of the one passed as argument. Functions that perform an action in particular write state in the Req object to make sure you are using the function correctly. For example, it's only possible to send one response, and to read the body once.

Exports

Connection:

Raw request:

Processed request:

Request body:

Response:

Stream handlers:

Types

push_opts()

push_opts() :: #{
    method => binary(),            %% case sensitive
    scheme => binary(),            %% lowercase; case insensitive
    host   => binary(),            %% lowercase; case insensitive
    port   => inet:port_number(),
    qs     => binary()             %% case sensitive
}

Push options.

By default, Cowboy will use the GET method, an empty query string, and take the scheme, host and port directly from the current request's URI.

read_body_opts()

read_body_opts() :: #{
    length  => non_neg_integer(),
    period  => non_neg_integer(),
    timeout => timeout()
}

Body reading options.

The defaults are function-specific.

req()

req() :: #{
    method  := binary(),               %% case sensitive
    version := cowboy:http_version() | atom(),
    scheme  := binary(),               %% lowercase; case insensitive
    host    := binary(),               %% lowercase; case insensitive
    port    := inet:port_number(),
    path    := binary(),               %% case sensitive
    qs      := binary(),               %% case sensitive
    headers := cowboy:http_headers(),
    peer    := {inet:ip_address(), inet:port_number()},
    sock    := {inet:ip_address(), inet:port_number()},
    cert    := binary() | undefined
}

The Req object.

Contains information about the request and response. While some fields are publicly documented, others aren't and shouldn't be used.

You may add custom fields if required. Make sure to namespace them by prepending an underscore and the name of your application:

Setting a custom field
Req#{'_myapp_auth_method' => pubkey}.

resp_body()

resp_body() :: iodata()
    | {sendfile, Offset, Length, Filename}

Offset   :: non_neg_integer()
Length   :: non_neg_integer()
Filename :: file:name_all()

Response body.

It can take two forms: the actual data to be sent or a tuple indicating which file to send.

When sending data directly, the type is either a binary or an iolist. Iolists are an efficient way to build output. Instead of concatenating strings or binaries, you can simply build a list containing the fragments you want to send in the order they should be sent:

Example iolists usage
1> RespBody = ["Hello ", [<<"world">>, $!]].
["Hello ",[<<"world">>,33]]
2> io:format("~s~n", [RespBody]).
Hello world!

Note that the length must be greater than zero for any data to be sent. Cowboy will send an empty body when the length is zero.

See also

cowboy(7)

Cowboy 2.8 Function Reference

Navigation

Version select

Like my work? Donate!

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.