| 1 |
= cowboy_req:read_part(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:read_part - Read the next multipart headers |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
read_part(Req :: cowboy_req:req()) |
| 12 |
-> read_part(Req, #{}) |
| 13 |
|
| 14 |
read_part(Req :: cowboy_req:req(), Opts) |
| 15 |
-> {ok, Headers, Req} | {done, Req} |
| 16 |
|
| 17 |
Opts :: cowboy_req:read_body_opts() |
| 18 |
Headers :: #{binary() => binary()} |
| 19 |
---- |
| 20 |
|
| 21 |
Read the next part of a multipart body. |
| 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 parses and returns |
| 26 |
headers. Examples of multipart media types are |
| 27 |
`multipart/form-data` and `multipart/byteranges`. |
| 28 |
|
| 29 |
Cowboy will skip any data remaining until the beginning of |
| 30 |
the next part. This includes the preamble to the multipart |
| 31 |
message but also the body of a previous part if it hasn't |
| 32 |
been read. Both are skipped automatically when calling this |
| 33 |
function. |
| 34 |
|
| 35 |
Cowboy will read the body before parsing in chunks of size |
| 36 |
up to 64KB, with a period of 5 seconds. This is tailored for |
| 37 |
reading part headers and might not be the most efficient for |
| 38 |
skipping the previous part's body. |
| 39 |
|
| 40 |
The headers returned are MIME headers, *NOT* HTTP headers. |
| 41 |
They can be parsed using the functions from the `cow_multipart` |
| 42 |
module. In addition, the `cow_multipart:form_data/1` function |
| 43 |
can be used to quickly extract information from `multipart/form-data` |
| 44 |
representations. |
| 45 |
|
| 46 |
// @todo Proper link to cow_multipart:form_data. |
| 47 |
|
| 48 |
Once a part has been read, it can not be read again. |
| 49 |
|
| 50 |
Once the body has been read, Cowboy sets the content-length |
| 51 |
header if it was not previously provided. |
| 52 |
|
| 53 |
// @todo Limit the maximum size of multipart headers. |
| 54 |
|
| 55 |
== Arguments |
| 56 |
|
| 57 |
Req:: |
| 58 |
|
| 59 |
The Req object. |
| 60 |
|
| 61 |
Opts:: |
| 62 |
|
| 63 |
A map of body reading options. Please refer to |
| 64 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] |
| 65 |
for details about each option. |
| 66 |
+ |
| 67 |
This function defaults the `length` to 64KB and the `period` |
| 68 |
to 5 seconds. |
| 69 |
|
| 70 |
== Return value |
| 71 |
|
| 72 |
An `ok` tuple is returned containing the next part's headers |
| 73 |
as a map. |
| 74 |
|
| 75 |
A `done` tuple is returned if there are no more parts to read. |
| 76 |
|
| 77 |
The Req object returned in the tuple must be used from that point |
| 78 |
onward. It contains a more up to date representation of the request. |
| 79 |
For example it may have an added content-length header once the |
| 80 |
body has been read. |
| 81 |
|
| 82 |
== Changelog |
| 83 |
|
| 84 |
* *2.0*: Function introduced. Replaces `part/1,2`. |
| 85 |
|
| 86 |
== Examples |
| 87 |
|
| 88 |
.Read all parts |
| 89 |
[source,erlang] |
| 90 |
---- |
| 91 |
acc_multipart(Req0, Acc) -> |
| 92 |
case cowboy_req:read_part(Req0) of |
| 93 |
{ok, Headers, Req1} -> |
| 94 |
{ok, Body, Req} = stream_body(Req1, <<>>), |
| 95 |
acc_multipart(Req, [{Headers, Body}|Acc]); |
| 96 |
{done, Req} -> |
| 97 |
{lists:reverse(Acc), Req} |
| 98 |
end. |
| 99 |
|
| 100 |
stream_body(Req0, Acc) -> |
| 101 |
case cowboy_req:read_part_body(Req0) of |
| 102 |
{more, Data, Req} -> |
| 103 |
stream_body(Req, << Acc/binary, Data/binary >>); |
| 104 |
{ok, Data, Req} -> |
| 105 |
{ok, << Acc/binary, Data/binary >>, Req} |
| 106 |
end. |
| 107 |
---- |
| 108 |
|
| 109 |
.Read all part headers, skipping bodies |
| 110 |
[source,erlang] |
| 111 |
---- |
| 112 |
skip_body_multipart(Req0, Acc) -> |
| 113 |
case cowboy_req:read_part(Req0) of |
| 114 |
{ok, Headers, Req} -> |
| 115 |
skip_body_multipart(Req, [Headers|Acc]); |
| 116 |
{done, Req} -> |
| 117 |
{lists:reverse(Acc), Req} |
| 118 |
end. |
| 119 |
---- |
| 120 |
|
| 121 |
.Read a part header in larger chunks |
| 122 |
[source,erlang] |
| 123 |
---- |
| 124 |
{ok, Headers, Req} = cowboy_req:read_part(Req0, #{length => 1000000}). |
| 125 |
---- |
| 126 |
|
| 127 |
== See also |
| 128 |
|
| 129 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 130 |
link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)], |
| 131 |
link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)], |
| 132 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)], |
| 133 |
link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)], |
| 134 |
link:man:cowboy_req:read_and_match_urlencoded_body(3)[cowboy_req:read_and_match_urlencoded_body(3)], |
| 135 |
link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)] |