| 1 |
= cowboy_req:read_part_body(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:read_part_body - Read the current part's body |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
read_part_body(Req :: cowboy_req:req()) |
| 12 |
-> read_part_body(Req, #{}) |
| 13 |
|
| 14 |
read_part_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 body of the current part of the multipart message. |
| 22 |
|
| 23 |
This function reads the request body and parses it as |
| 24 |
multipart. Each parts of a multipart representation have |
| 25 |
their own headers and body. This function returns the |
| 26 |
body of the current part. Examples of multipart media types |
| 27 |
are `multipart/form-data` and `multipart/byteranges`. |
| 28 |
|
| 29 |
This function reads a chunk of the part's body. A `more` tuple |
| 30 |
is returned when more data remains to be read. Call the function |
| 31 |
repeatedly until an `ok` tuple is returned to read the entire body. |
| 32 |
|
| 33 |
Once a part has been read, it can not be read again. |
| 34 |
|
| 35 |
Once the body has been read, Cowboy sets the content-length |
| 36 |
header if it was not previously provided. |
| 37 |
|
| 38 |
// @todo Limit the maximum size of multipart headers. |
| 39 |
|
| 40 |
== Arguments |
| 41 |
|
| 42 |
Req:: |
| 43 |
|
| 44 |
The Req object. |
| 45 |
|
| 46 |
Opts:: |
| 47 |
|
| 48 |
A map of body reading options. Please refer to |
| 49 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] |
| 50 |
for details about each option. |
| 51 |
+ |
| 52 |
This function uses the same default options as the |
| 53 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] |
| 54 |
function. |
| 55 |
|
| 56 |
== Return value |
| 57 |
|
| 58 |
A `more` tuple is returned when there are more data to be read. |
| 59 |
|
| 60 |
An `ok` tuple is returned when there are no more data to be read. |
| 61 |
|
| 62 |
The data is always returned as a binary. |
| 63 |
|
| 64 |
The Req object returned in the tuple must be used from that point |
| 65 |
onward. It contains a more up to date representation of the request. |
| 66 |
For example it may have an added content-length header once the |
| 67 |
body has been read. |
| 68 |
|
| 69 |
== Changelog |
| 70 |
|
| 71 |
* *2.0*: Function introduced. Replaces `part_body/1,2`. |
| 72 |
|
| 73 |
== Examples |
| 74 |
|
| 75 |
.Read a full part's body |
| 76 |
[source,erlang] |
| 77 |
---- |
| 78 |
stream_body(Req0, Acc) -> |
| 79 |
case cowboy_req:read_part_body(Req0) of |
| 80 |
{more, Data, Req} -> |
| 81 |
stream_body(Req, << Acc/binary, Data/binary >>); |
| 82 |
{ok, Data, Req} -> |
| 83 |
{ok, << Acc/binary, Data/binary >>, Req} |
| 84 |
end. |
| 85 |
---- |
| 86 |
|
| 87 |
.Ensure a part's body is smaller than 64KB |
| 88 |
[source,erlang] |
| 89 |
---- |
| 90 |
{ok, Body, Req} = cowboy_req:read_part_body(Req0, #{length => 64000}). |
| 91 |
---- |
| 92 |
|
| 93 |
== See also |
| 94 |
|
| 95 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 96 |
link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)], |
| 97 |
link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)], |
| 98 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)], |
| 99 |
link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)], |
| 100 |
link:man:cowboy_req:read_and_match_urlencoded_body(3)[cowboy_req:read_and_match_urlencoded_body(3)], |
| 101 |
link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)] |