| 1 |
[[req_body]] |
| 2 |
== Reading the request body |
| 3 |
|
| 4 |
The request body can be read using the Req object. |
| 5 |
|
| 6 |
Cowboy will not attempt to read the body until requested. |
| 7 |
You need to call the body reading functions in order to |
| 8 |
retrieve it. |
| 9 |
|
| 10 |
Cowboy will not cache the body, it is therefore only |
| 11 |
possible to read it once. |
| 12 |
|
| 13 |
You are not required to read it, however. If a body is |
| 14 |
present and was not read, Cowboy will either cancel or |
| 15 |
skip its download, depending on the protocol. |
| 16 |
|
| 17 |
Cowboy provides functions for reading the body raw, |
| 18 |
and read and parse form urlencoded or xref:multipart[multipart bodies]. |
| 19 |
The latter is covered in its own chapter. |
| 20 |
|
| 21 |
=== Request body presence |
| 22 |
|
| 23 |
Not all requests come with a body. You can check for |
| 24 |
the presence of a request body with this function: |
| 25 |
|
| 26 |
[source,erlang] |
| 27 |
cowboy_req:has_body(Req). |
| 28 |
|
| 29 |
It returns `true` if there is a body; `false` otherwise. |
| 30 |
|
| 31 |
In practice, this function is rarely used. When the |
| 32 |
method is `POST`, `PUT` or `PATCH`, the request body |
| 33 |
is often required by the application, which should |
| 34 |
just attempt to read it directly. |
| 35 |
|
| 36 |
=== Request body length |
| 37 |
|
| 38 |
You can obtain the length of the body: |
| 39 |
|
| 40 |
[source,erlang] |
| 41 |
Length = cowboy_req:body_length(Req). |
| 42 |
|
| 43 |
Note that the length may not be known in advance. In |
| 44 |
that case `undefined` will be returned. This can happen |
| 45 |
with HTTP/1.1's chunked transfer-encoding, or HTTP/2 |
| 46 |
when no content-length was provided. |
| 47 |
|
| 48 |
Cowboy will update the body length in the Req object |
| 49 |
once the body has been read completely. A length will |
| 50 |
always be returned when attempting to call this function |
| 51 |
after reading the body completely. |
| 52 |
|
| 53 |
=== Reading the body |
| 54 |
|
| 55 |
You can read the entire body with one function call: |
| 56 |
|
| 57 |
[source,erlang] |
| 58 |
{ok, Data, Req} = cowboy_req:read_body(Req0). |
| 59 |
|
| 60 |
Cowboy returns an `ok` tuple when the body has been |
| 61 |
read fully. |
| 62 |
|
| 63 |
By default, Cowboy will attempt to read up to 8MB |
| 64 |
of data, for up to 15 seconds. The call will return |
| 65 |
once Cowboy has read at least 8MB of data, or at |
| 66 |
the end of the 15 seconds period. |
| 67 |
|
| 68 |
These values can be customized. For example, to read |
| 69 |
only up to 1MB for up to 5 seconds: |
| 70 |
|
| 71 |
[source,erlang] |
| 72 |
---- |
| 73 |
{ok, Data, Req} = cowboy_req:read_body(Req0, |
| 74 |
#{length => 1000000, period => 5000}). |
| 75 |
---- |
| 76 |
|
| 77 |
These two options can effectively be used to control |
| 78 |
the rate of transmission of the request body. |
| 79 |
|
| 80 |
It is also possible to asynchronously read the request |
| 81 |
body using auto mode: |
| 82 |
|
| 83 |
[source,erlang] |
| 84 |
---- |
| 85 |
Ref = make_ref(), |
| 86 |
cowboy_req:cast({read_body, self(), Ref, auto, infinity}, Req). |
| 87 |
---- |
| 88 |
|
| 89 |
Cowboy will wait indefinitely for data and then send a |
| 90 |
`request_body` message as soon as it has data available, |
| 91 |
regardless of length. |
| 92 |
|
| 93 |
[source,erlang] |
| 94 |
---- |
| 95 |
receive |
| 96 |
{request_body, Ref, nofin, Data} -> |
| 97 |
do_something(Data); |
| 98 |
{request_body, Ref, fin, _BodyLen, Data} -> |
| 99 |
do_something(Data) |
| 100 |
end. |
| 101 |
---- |
| 102 |
|
| 103 |
Asynchronous reading of data pairs well with loop handlers. |
| 104 |
|
| 105 |
=== Streaming the body |
| 106 |
|
| 107 |
When the body is too large, the first call will return |
| 108 |
a `more` tuple instead of `ok`. You can call the |
| 109 |
function again to read more of the body, reading |
| 110 |
it one chunk at a time. |
| 111 |
|
| 112 |
[source,erlang] |
| 113 |
---- |
| 114 |
read_body_to_console(Req0) -> |
| 115 |
case cowboy_req:read_body(Req0) of |
| 116 |
{ok, Data, Req} -> |
| 117 |
io:format("~s", [Data]), |
| 118 |
Req; |
| 119 |
{more, Data, Req} -> |
| 120 |
io:format("~s", [Data]), |
| 121 |
read_body_to_console(Req) |
| 122 |
end. |
| 123 |
---- |
| 124 |
|
| 125 |
The `length` and `period` options can also be used. |
| 126 |
They need to be passed for every call. |
| 127 |
|
| 128 |
=== Reading a form urlencoded body |
| 129 |
|
| 130 |
Cowboy provides a convenient function for reading and |
| 131 |
parsing bodies sent as application/x-www-form-urlencoded. |
| 132 |
|
| 133 |
[source,erlang] |
| 134 |
{ok, KeyValues, Req} = cowboy_req:read_urlencoded_body(Req0). |
| 135 |
|
| 136 |
This function returns a list of key/values, exactly like |
| 137 |
the function `cowboy_req:parse_qs/1`. |
| 138 |
|
| 139 |
The defaults for this function are different. Cowboy will |
| 140 |
read for up to 64KB and up to 5 seconds. They can be modified: |
| 141 |
|
| 142 |
[source,erlang] |
| 143 |
---- |
| 144 |
{ok, KeyValues, Req} = cowboy_req:read_urlencoded_body(Req0, |
| 145 |
#{length => 4096, period => 3000}). |
| 146 |
---- |