| 1 |
= cowboy_req:read_urlencoded_body(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:read_urlencoded_body - Read and parse a urlencoded request body |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
read_urlencoded_body(Req :: cowboy_req:req()) |
| 12 |
-> read_urlencoded_body(Req, #{}) |
| 13 |
|
| 14 |
read_urlencoded_body(Req :: cowboy_req:req(), Opts) |
| 15 |
-> {ok, Body, Req} |
| 16 |
|
| 17 |
Opts :: cowboy_req:read_body_opts() |
| 18 |
Body :: [{Key :: binary(), Value :: binary() | true}] |
| 19 |
---- |
| 20 |
|
| 21 |
Read and parse a urlencoded request body. |
| 22 |
|
| 23 |
This function reads the request body and parses it as |
| 24 |
`application/x-www-form-urlencoded`. It returns a list |
| 25 |
of key/values. |
| 26 |
|
| 27 |
The urlencoded media type is used by Web browsers when |
| 28 |
submitting HTML forms using the POST method. |
| 29 |
|
| 30 |
Cowboy needs to read the full body before parsing. By default |
| 31 |
it will read bodies of size up to 64KB. It is possible to |
| 32 |
provide options to read larger bodies if required. |
| 33 |
|
| 34 |
Cowboy will automatically handle protocol details including |
| 35 |
the expect header, chunked transfer-encoding and others. |
| 36 |
|
| 37 |
Once the body has been read, Cowboy sets the content-length |
| 38 |
header if it was not previously provided. |
| 39 |
|
| 40 |
This function can only be called once. Calling it again will |
| 41 |
result in undefined behavior. |
| 42 |
|
| 43 |
== Arguments |
| 44 |
|
| 45 |
Req:: |
| 46 |
|
| 47 |
The Req object. |
| 48 |
|
| 49 |
Opts:: |
| 50 |
|
| 51 |
A map of body reading options. Please refer to |
| 52 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] |
| 53 |
for details about each option. |
| 54 |
+ |
| 55 |
This function defaults the `length` to 64KB, the `period` |
| 56 |
to 5 seconds and `max_keys` to 100. |
| 57 |
|
| 58 |
== Return value |
| 59 |
|
| 60 |
An `ok` tuple is returned containing a list of key/values found |
| 61 |
in the body. |
| 62 |
|
| 63 |
The Req object returned in the tuple must be used from that point |
| 64 |
onward. It contains a more up to date representation of the request. |
| 65 |
For example it may have an added content-length header once the |
| 66 |
body has been read. |
| 67 |
|
| 68 |
== Changelog |
| 69 |
|
| 70 |
* *2.17*: Option `max_keys` added. |
| 71 |
* *2.0*: Function introduced. Replaces `body_qs/1,2`. |
| 72 |
|
| 73 |
== Examples |
| 74 |
|
| 75 |
.Read a urlencoded body |
| 76 |
[source,erlang] |
| 77 |
---- |
| 78 |
{ok, Body, Req} = cowboy_req:read_urlencoded_body(Req0), |
| 79 |
{_, Lang} = lists:keyfind(<<"lang">>, 1, Body). |
| 80 |
---- |
| 81 |
|
| 82 |
.Allow large urlencoded bodies |
| 83 |
[source,erlang] |
| 84 |
---- |
| 85 |
{ok, Body, Req} = cowboy_req:read_urlencoded_body(Req0, #{length => 1000000}). |
| 86 |
---- |
| 87 |
|
| 88 |
== See also |
| 89 |
|
| 90 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 91 |
link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)], |
| 92 |
link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)], |
| 93 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)], |
| 94 |
link:man:cowboy_req:read_and_match_urlencoded_body(3)[cowboy_req:read_and_match_urlencoded_body(3)], |
| 95 |
link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)], |
| 96 |
link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)] |