| 1 |
= cowboy_req(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req - HTTP request and response |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
The module `cowboy_req` provides functions to access, manipulate |
| 10 |
and respond to requests. |
| 11 |
|
| 12 |
There are four types of functions in this module. They can be |
| 13 |
differentiated by their name and their return type: |
| 14 |
|
| 15 |
[options="header"] |
| 16 |
|=== |
| 17 |
| Type | Name pattern | Return type |
| 18 |
| access | no verb, parse_*, match_* | `Value` |
| 19 |
| question | has_* | `boolean()` |
| 20 |
| modification | set_* | `Req` |
| 21 |
| action | any other verb | `ok \| {Result, Value, Req}` |
| 22 |
|=== |
| 23 |
|
| 24 |
Any `Req` returned must be used in place of the one passed as |
| 25 |
argument. Functions that perform an action in particular write |
| 26 |
state in the Req object to make sure you are using the function |
| 27 |
correctly. For example, it's only possible to send one response, |
| 28 |
and to read the body once. |
| 29 |
|
| 30 |
== Exports |
| 31 |
|
| 32 |
Connection: |
| 33 |
|
| 34 |
* link:man:cowboy_req:peer(3)[cowboy_req:peer(3)] - Peer address and port |
| 35 |
* link:man:cowboy_req:sock(3)[cowboy_req:sock(3)] - Socket address and port |
| 36 |
* link:man:cowboy_req:cert(3)[cowboy_req:cert(3)] - Client TLS certificate |
| 37 |
|
| 38 |
Raw request: |
| 39 |
|
| 40 |
* link:man:cowboy_req:method(3)[cowboy_req:method(3)] - HTTP method |
| 41 |
* link:man:cowboy_req:version(3)[cowboy_req:version(3)] - HTTP version |
| 42 |
* link:man:cowboy_req:scheme(3)[cowboy_req:scheme(3)] - URI scheme |
| 43 |
* link:man:cowboy_req:host(3)[cowboy_req:host(3)] - URI host name |
| 44 |
* link:man:cowboy_req:port(3)[cowboy_req:port(3)] - URI port number |
| 45 |
* link:man:cowboy_req:path(3)[cowboy_req:path(3)] - URI path |
| 46 |
* link:man:cowboy_req:qs(3)[cowboy_req:qs(3)] - URI query string |
| 47 |
* link:man:cowboy_req:uri(3)[cowboy_req:uri(3)] - Reconstructed URI |
| 48 |
* link:man:cowboy_req:header(3)[cowboy_req:header(3)] - HTTP header |
| 49 |
* link:man:cowboy_req:headers(3)[cowboy_req:headers(3)] - HTTP headers |
| 50 |
|
| 51 |
Processed request: |
| 52 |
|
| 53 |
* link:man:cowboy_req:parse_qs(3)[cowboy_req:parse_qs(3)] - Parse the query string |
| 54 |
* link:man:cowboy_req:match_qs(3)[cowboy_req:match_qs(3)] - Match the query string against constraints |
| 55 |
* link:man:cowboy_req:parse_header(3)[cowboy_req:parse_header(3)] - Parse the given HTTP header |
| 56 |
* link:man:cowboy_req:filter_cookies(3)[cowboy_req:filter_cookies(3)] - Filter cookie headers |
| 57 |
* link:man:cowboy_req:parse_cookies(3)[cowboy_req:parse_cookies(3)] - Parse cookie headers |
| 58 |
* link:man:cowboy_req:match_cookies(3)[cowboy_req:match_cookies(3)] - Match cookies against constraints |
| 59 |
* link:man:cowboy_req:binding(3)[cowboy_req:binding(3)] - Access a value bound from the route |
| 60 |
* link:man:cowboy_req:bindings(3)[cowboy_req:bindings(3)] - Access all values bound from the route |
| 61 |
* link:man:cowboy_req:host_info(3)[cowboy_req:host_info(3)] - Access the route's heading host segments |
| 62 |
* link:man:cowboy_req:path_info(3)[cowboy_req:path_info(3)] - Access the route's trailing path segments |
| 63 |
|
| 64 |
Request body: |
| 65 |
|
| 66 |
* link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)] - Is there a request body? |
| 67 |
* link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)] - Body length |
| 68 |
* link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] - Read the request body |
| 69 |
* link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)] - Read and parse a urlencoded request body |
| 70 |
* link:man:cowboy_req:read_and_match_urlencoded_body(3)[cowboy_req:read_and_match_urlencoded_body(3)] - Read, parse and match a urlencoded request body against constraints |
| 71 |
* link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)] - Read the next multipart headers |
| 72 |
* link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)] - Read the current part's body |
| 73 |
|
| 74 |
Response: |
| 75 |
|
| 76 |
* link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)] - Set a cookie |
| 77 |
* link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)] - Set a response header |
| 78 |
* link:man:cowboy_req:set_resp_headers(3)[cowboy_req:set_resp_headers(3)] - Set several response headers |
| 79 |
* link:man:cowboy_req:has_resp_header(3)[cowboy_req:has_resp_header(3)] - Is the given response header set? |
| 80 |
* link:man:cowboy_req:resp_header(3)[cowboy_req:resp_header(3)] - Response header |
| 81 |
* link:man:cowboy_req:resp_headers(3)[cowboy_req:resp_headers(3)] - Response headers |
| 82 |
* link:man:cowboy_req:delete_resp_header(3)[cowboy_req:delete_resp_header(3)] - Delete a response header |
| 83 |
* link:man:cowboy_req:set_resp_body(3)[cowboy_req:set_resp_body(3)] - Set the response body |
| 84 |
* link:man:cowboy_req:has_resp_body(3)[cowboy_req:has_resp_body(3)] - Is there a response body? |
| 85 |
* link:man:cowboy_req:inform(3)[cowboy_req:inform(3)] - Send an informational response |
| 86 |
* link:man:cowboy_req:reply(3)[cowboy_req:reply(3)] - Send the response |
| 87 |
* link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)] - Send the response headers |
| 88 |
* link:man:cowboy_req:stream_body(3)[cowboy_req:stream_body(3)] - Stream the response body |
| 89 |
* link:man:cowboy_req:stream_events(3)[cowboy_req:stream_events(3)] - Stream events |
| 90 |
* link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] - Send the response trailers |
| 91 |
* link:man:cowboy_req:push(3)[cowboy_req:push(3)] - Push a resource to the client |
| 92 |
|
| 93 |
Stream handlers: |
| 94 |
|
| 95 |
* link:man:cowboy_req:cast(3)[cowboy_req:cast(3)] - Cast a stream handler event |
| 96 |
|
| 97 |
== Types |
| 98 |
|
| 99 |
=== push_opts() |
| 100 |
|
| 101 |
[source,erlang] |
| 102 |
---- |
| 103 |
push_opts() :: #{ |
| 104 |
method => binary(), %% case sensitive |
| 105 |
scheme => binary(), %% lowercase; case insensitive |
| 106 |
host => binary(), %% lowercase; case insensitive |
| 107 |
port => inet:port_number(), |
| 108 |
qs => binary() %% case sensitive |
| 109 |
} |
| 110 |
---- |
| 111 |
|
| 112 |
Push options. |
| 113 |
|
| 114 |
By default, Cowboy will use the GET method, an empty query string, |
| 115 |
and take the scheme, host and port directly from the current |
| 116 |
request's URI. |
| 117 |
|
| 118 |
=== read_body_opts() |
| 119 |
|
| 120 |
[source,erlang] |
| 121 |
---- |
| 122 |
read_body_opts() :: #{ |
| 123 |
length => non_neg_integer() | auto, |
| 124 |
max_keys => non_neg_integer(), |
| 125 |
period => non_neg_integer() | infinity, |
| 126 |
timeout => timeout() |
| 127 |
} |
| 128 |
---- |
| 129 |
|
| 130 |
Body reading options. |
| 131 |
|
| 132 |
The defaults are function-specific. |
| 133 |
|
| 134 |
Auto mode can be enabled by setting `length` to `auto` |
| 135 |
and `period` to `infinity`. The period cannot be set |
| 136 |
to `infinity` when auto mode isn't used. |
| 137 |
|
| 138 |
=== req() |
| 139 |
|
| 140 |
[source,erlang] |
| 141 |
---- |
| 142 |
req() :: #{ |
| 143 |
method := binary(), %% case sensitive |
| 144 |
version := cowboy:http_version() | atom(), |
| 145 |
scheme := binary(), %% lowercase; case insensitive |
| 146 |
host := binary(), %% lowercase; case insensitive |
| 147 |
port := inet:port_number(), |
| 148 |
path := binary(), %% case sensitive |
| 149 |
qs := binary(), %% case sensitive |
| 150 |
headers := cowboy:http_headers(), |
| 151 |
peer := {inet:ip_address(), inet:port_number()}, |
| 152 |
sock := {inet:ip_address(), inet:port_number()}, |
| 153 |
cert := binary() | undefined |
| 154 |
} |
| 155 |
---- |
| 156 |
|
| 157 |
The Req object. |
| 158 |
|
| 159 |
Contains information about the request and response. While |
| 160 |
some fields are publicly documented, others aren't and shouldn't |
| 161 |
be used. |
| 162 |
|
| 163 |
You may add custom fields if required. Make sure to namespace |
| 164 |
them by prepending an underscore and the name of your application: |
| 165 |
|
| 166 |
.Setting a custom field |
| 167 |
[source,erlang] |
| 168 |
---- |
| 169 |
Req#{'_myapp_auth_method' => pubkey}. |
| 170 |
---- |
| 171 |
|
| 172 |
=== resp_body() |
| 173 |
|
| 174 |
[source,erlang] |
| 175 |
---- |
| 176 |
resp_body() :: iodata() |
| 177 |
| {sendfile, Offset, Length, Filename} |
| 178 |
|
| 179 |
Offset :: non_neg_integer() |
| 180 |
Length :: non_neg_integer() |
| 181 |
Filename :: file:name_all() |
| 182 |
---- |
| 183 |
|
| 184 |
Response body. |
| 185 |
|
| 186 |
It can take two forms: the actual data to be sent or a |
| 187 |
tuple indicating which file to send. |
| 188 |
|
| 189 |
When sending data directly, the type is either a binary or |
| 190 |
an iolist. Iolists are an efficient way to build output. |
| 191 |
Instead of concatenating strings or binaries, you can simply |
| 192 |
build a list containing the fragments you want to send in the |
| 193 |
order they should be sent: |
| 194 |
|
| 195 |
.Example iolists usage |
| 196 |
[source,erlang] |
| 197 |
---- |
| 198 |
1> RespBody = ["Hello ", [<<"world">>, $!]]. |
| 199 |
["Hello ",[<<"world">>,33]] |
| 200 |
2> io:format("~s~n", [RespBody]). |
| 201 |
Hello world! |
| 202 |
---- |
| 203 |
|
| 204 |
Note that the length must be greater than zero for any data |
| 205 |
to be sent. Cowboy will send an empty body when the length |
| 206 |
is zero. |
| 207 |
|
| 208 |
== See also |
| 209 |
|
| 210 |
link:man:cowboy(7)[cowboy(7)] |