| 1 |
= cowboy_req:read_body(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:read_body - Read the request body |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
read_body(Req :: cowboy_req:req()) |
| 12 |
-> read_body(Req, #{}) |
| 13 |
|
| 14 |
read_body(Req :: cowboy_req:req(), Opts) |
| 15 |
-> {ok, Data :: binary(), Req} |
| 16 |
| {more, Data :: binary(), Req} |
| 17 |
|
| 18 |
Opts :: cowboy_req:read_body_opts() |
| 19 |
---- |
| 20 |
|
| 21 |
Read the request body. |
| 22 |
|
| 23 |
This function reads a chunk of the request body. A `more` tuple |
| 24 |
is returned when more data remains to be read. Call the function |
| 25 |
repeatedly until an `ok` tuple is returned to read the entire body. |
| 26 |
|
| 27 |
An `ok` tuple with empty data is returned when the request has no body, |
| 28 |
or when calling this function again after the body has already |
| 29 |
been read. It is therefore safe to call this function directly. |
| 30 |
Note that the body can only be read once. |
| 31 |
|
| 32 |
This function reads the request body from the connection process. |
| 33 |
The connection process is responsible for reading from the socket. |
| 34 |
The exact behavior varies depending on the protocol. |
| 35 |
|
| 36 |
The options therefore are only related to the communication |
| 37 |
between the request process and the connection process. |
| 38 |
|
| 39 |
Cowboy will automatically handle protocol details including |
| 40 |
the expect header, chunked transfer-encoding and others. |
| 41 |
|
| 42 |
Once the body has been read fully, Cowboy sets the content-length |
| 43 |
header if it was not previously provided. |
| 44 |
|
| 45 |
== Arguments |
| 46 |
|
| 47 |
Req:: |
| 48 |
|
| 49 |
The Req object. |
| 50 |
|
| 51 |
Opts:: |
| 52 |
|
| 53 |
A map of body reading options. |
| 54 |
+ |
| 55 |
The `length` option can be used to request smaller or bigger |
| 56 |
chunks of data to be sent. It is a best effort approach, Cowboy |
| 57 |
may send more data than configured on occasions. It defaults |
| 58 |
to 8MB. |
| 59 |
+ |
| 60 |
The `period` indicates how long the connection process will wait |
| 61 |
before it provides us with the data it received. It defaults |
| 62 |
to 15 seconds. |
| 63 |
+ |
| 64 |
The connection process sends data to the request process when |
| 65 |
either the `length` of data or the `period` of time is reached. |
| 66 |
+ |
| 67 |
The `timeout` option is a safeguard in case the connection |
| 68 |
process becomes unresponsive. The function will crash if no |
| 69 |
message was received in that interval. The timeout should be |
| 70 |
larger than the period. It defaults to the period + 1 second. |
| 71 |
+ |
| 72 |
Auto mode can be enabled by setting the `length` to `auto` and |
| 73 |
the `period` to `infinity`. When auto mode is used, Cowboy will |
| 74 |
send data to the handler as soon as it receives it, regardless |
| 75 |
of its size. It will wait indefinitely until data is available. |
| 76 |
Auto mode's main purpose is asynchronous body reading using |
| 77 |
link:man:cowboy_req:cast(3)[cowboy_req:cast(3)]. |
| 78 |
|
| 79 |
== Return value |
| 80 |
|
| 81 |
A `more` tuple is returned when there are more data to be read. |
| 82 |
|
| 83 |
An `ok` tuple is returned when there are no more data to be read, |
| 84 |
either because this is the last chunk of data, the body has already |
| 85 |
been read, or there was no body to begin with. |
| 86 |
|
| 87 |
The data is always returned as a binary. |
| 88 |
|
| 89 |
The Req object returned in the tuple must be used from that point |
| 90 |
onward. It contains a more up to date representation of the request. |
| 91 |
For example it may have an added content-length header once the |
| 92 |
body has been read. |
| 93 |
|
| 94 |
== Changelog |
| 95 |
|
| 96 |
* *2.11*: The `length` option now accepts `auto` and the |
| 97 |
period now accepts `infinity`. This adds support for |
| 98 |
reading the body in auto mode. |
| 99 |
* *2.0*: Function introduced. Replaces `body/1,2`. |
| 100 |
|
| 101 |
== Examples |
| 102 |
|
| 103 |
.Read the entire body |
| 104 |
[source,erlang] |
| 105 |
---- |
| 106 |
read_body(Req0, Acc) -> |
| 107 |
case cowboy_req:read_body(Req0) of |
| 108 |
{ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req}; |
| 109 |
{more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>) |
| 110 |
end. |
| 111 |
---- |
| 112 |
|
| 113 |
.Read the body in small chunks |
| 114 |
[source,erlang] |
| 115 |
---- |
| 116 |
cowboy_req:read_body(Req, #{length => 64000}). |
| 117 |
---- |
| 118 |
|
| 119 |
== See also |
| 120 |
|
| 121 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 122 |
link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)], |
| 123 |
link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)], |
| 124 |
link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)], |
| 125 |
link:man:cowboy_req:read_and_match_urlencoded_body(3)[cowboy_req:read_and_match_urlencoded_body(3)], |
| 126 |
link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)], |
| 127 |
link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)] |