1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
= 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:
[options="header"]
|===
| 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:
* link:man:cowboy_req:peer(3)[cowboy_req:peer(3)] - Peer address and port
* link:man:cowboy_req:sock(3)[cowboy_req:sock(3)] - Socket address and port
* link:man:cowboy_req:cert(3)[cowboy_req:cert(3)] - Client TLS certificate
Raw request:
* link:man:cowboy_req:method(3)[cowboy_req:method(3)] - HTTP method
* link:man:cowboy_req:version(3)[cowboy_req:version(3)] - HTTP version
* link:man:cowboy_req:scheme(3)[cowboy_req:scheme(3)] - URI scheme
* link:man:cowboy_req:host(3)[cowboy_req:host(3)] - URI host name
* link:man:cowboy_req:port(3)[cowboy_req:port(3)] - URI port number
* link:man:cowboy_req:path(3)[cowboy_req:path(3)] - URI path
* link:man:cowboy_req:qs(3)[cowboy_req:qs(3)] - URI query string
* link:man:cowboy_req:uri(3)[cowboy_req:uri(3)] - Reconstructed URI
* link:man:cowboy_req:header(3)[cowboy_req:header(3)] - HTTP header
* link:man:cowboy_req:headers(3)[cowboy_req:headers(3)] - HTTP headers
Processed request:
* link:man:cowboy_req:parse_qs(3)[cowboy_req:parse_qs(3)] - Parse the query string
* link:man:cowboy_req:match_qs(3)[cowboy_req:match_qs(3)] - Match the query string against constraints
* link:man:cowboy_req:parse_header(3)[cowboy_req:parse_header(3)] - Parse the given HTTP header
* link:man:cowboy_req:parse_cookies(3)[cowboy_req:parse_cookies(3)] - Parse cookie headers
* link:man:cowboy_req:match_cookies(3)[cowboy_req:match_cookies(3)] - Match cookies against constraints
* link:man:cowboy_req:binding(3)[cowboy_req:binding(3)] - Access a value bound from the route
* link:man:cowboy_req:bindings(3)[cowboy_req:bindings(3)] - Access all values bound from the route
* link:man:cowboy_req:host_info(3)[cowboy_req:host_info(3)] - Access the route's heading host segments
* link:man:cowboy_req:path_info(3)[cowboy_req:path_info(3)] - Access the route's trailing path segments
Request body:
* link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)] - Is there a request body?
* link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)] - Body length
* link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] - Read the request body
* link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)] - Read and parse a urlencoded request body
* link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)] - Read the next multipart headers
* link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)] - Read the current part's body
Response:
* link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)] - Set a cookie
* link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)] - Set a response header
* link:man:cowboy_req:set_resp_headers(3)[cowboy_req:set_resp_headers(3)] - Set several response headers
* link:man:cowboy_req:has_resp_header(3)[cowboy_req:has_resp_header(3)] - Is the given response header set?
* link:man:cowboy_req:resp_header(3)[cowboy_req:resp_header(3)] - Response header
* link:man:cowboy_req:resp_headers(3)[cowboy_req:resp_headers(3)] - Response headers
* link:man:cowboy_req:delete_resp_header(3)[cowboy_req:delete_resp_header(3)] - Delete a response header
* link:man:cowboy_req:set_resp_body(3)[cowboy_req:set_resp_body(3)] - Set the response body
* link:man:cowboy_req:has_resp_body(3)[cowboy_req:has_resp_body(3)] - Is there a response body?
* link:man:cowboy_req:inform(3)[cowboy_req:inform(3)] - Send an informational response
* link:man:cowboy_req:reply(3)[cowboy_req:reply(3)] - Send the response
* link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)] - Send the response headers
* link:man:cowboy_req:stream_body(3)[cowboy_req:stream_body(3)] - Stream the response body
* link:man:cowboy_req:push(3)[cowboy_req:push(3)] - Push a resource to the client
== Types
=== push_opts()
[source,erlang]
----
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()
[source,erlang]
----
read_body_opts() :: #{
length => non_neg_integer(),
period => non_neg_integer(),
timeout => timeout()
}
----
Body reading options.
The defaults are function-specific.
=== req()
[source,erlang]
----
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
[source,erlang]
----
Req#{_myapp_auth_method => pubkey}.
----
=== resp_body()
[source,erlang]
----
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
[source,erlang]
----
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
link:man:cowboy(7)[cowboy(7)]
|