2016-01-14 13:35:25 +01:00
|
|
|
[[req_body]]
|
|
|
|
== Reading the request body
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
The request body can be read using the Req object.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
Cowboy will not attempt to read the body until requested.
|
|
|
|
You need to call the body reading functions in order to
|
|
|
|
retrieve it.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
Cowboy will not cache the body, it is therefore only
|
|
|
|
possible to read it once.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
You are not required to read it, however. If a body is
|
|
|
|
present and was not read, Cowboy will either cancel or
|
|
|
|
skip its download, depending on the protocol.
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
Cowboy provides functions for reading the body raw,
|
2016-08-30 13:23:31 +02:00
|
|
|
and read and parse form urlencoded or xref:multipart[multipart bodies].
|
2016-08-25 17:40:37 +02:00
|
|
|
The latter is covered in its own chapter.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
=== Request body presence
|
|
|
|
|
|
|
|
Not all requests come with a body. You can check for
|
|
|
|
the presence of a request body with this function:
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2013-09-23 15:44:10 +02:00
|
|
|
cowboy_req:has_body(Req).
|
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
It returns `true` if there is a body; `false` otherwise.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
In practice, this function is rarely used. When the
|
|
|
|
method is `POST`, `PUT` or `PATCH`, the request body
|
|
|
|
is often required by the application, which should
|
|
|
|
just attempt to read it directly.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Request body length
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
You can obtain the length of the body:
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2014-09-23 16:43:29 +03:00
|
|
|
Length = cowboy_req:body_length(Req).
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
Note that the length may not be known in advance. In
|
|
|
|
that case `undefined` will be returned. This can happen
|
|
|
|
with HTTP/1.1's chunked transfer-encoding, or HTTP/2
|
|
|
|
when no content-length was provided.
|
|
|
|
|
|
|
|
Cowboy will update the body length in the Req object
|
|
|
|
once the body has been read completely. A length will
|
|
|
|
always be returned when attempting to call this function
|
|
|
|
after reading the body completely.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Reading the body
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
You can read the entire body with one function call:
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2016-08-25 17:40:37 +02:00
|
|
|
{ok, Data, Req} = cowboy_req:read_body(Req0).
|
|
|
|
|
|
|
|
Cowboy returns an `ok` tuple when the body has been
|
|
|
|
read fully.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
By default, Cowboy will attempt to read up to 8MB
|
|
|
|
of data, for up to 15 seconds. The call will return
|
|
|
|
once Cowboy has read at least 8MB of data, or at
|
|
|
|
the end of the 15 seconds period.
|
|
|
|
|
|
|
|
These values can be customized. For example, to read
|
|
|
|
only up to 1MB for up to 5 seconds:
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2016-08-25 17:40:37 +02:00
|
|
|
----
|
|
|
|
{ok, Data, Req} = cowboy_req:read_body(Req0,
|
2016-09-14 18:51:11 +02:00
|
|
|
#{length => 1000000, period => 5000}).
|
2016-08-25 17:40:37 +02:00
|
|
|
----
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2024-01-08 15:13:18 +01:00
|
|
|
These two options can effectively be used to control
|
|
|
|
the rate of transmission of the request body.
|
|
|
|
|
|
|
|
It is also possible to asynchronously read the request
|
|
|
|
body using auto mode:
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2024-01-08 15:13:18 +01:00
|
|
|
----
|
|
|
|
Ref = make_ref(),
|
|
|
|
cowboy_req:cast({read_body, self(), Ref, auto, infinity}, Req).
|
|
|
|
----
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2024-01-08 15:13:18 +01:00
|
|
|
Cowboy will wait indefinitely for data and then send a
|
|
|
|
`request_body` message as soon as it has data available,
|
|
|
|
regardless of length.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2024-01-08 15:13:18 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
|
|
|
receive
|
|
|
|
{request_body, Ref, nofin, Data} ->
|
|
|
|
do_something(Data);
|
|
|
|
{request_body, Ref, fin, _BodyLen, Data} ->
|
|
|
|
do_something(Data)
|
|
|
|
end.
|
|
|
|
----
|
|
|
|
|
|
|
|
Asynchronous reading of data pairs well with loop handlers.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Streaming the body
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
When the body is too large, the first call will return
|
|
|
|
a `more` tuple instead of `ok`. You can call the
|
|
|
|
function again to read more of the body, reading
|
|
|
|
it one chunk at a time.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
2016-08-25 17:40:37 +02:00
|
|
|
read_body_to_console(Req0) ->
|
|
|
|
case cowboy_req:read_body(Req0) of
|
|
|
|
{ok, Data, Req} ->
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
io:format("~s", [Data]),
|
2016-08-25 17:40:37 +02:00
|
|
|
Req;
|
|
|
|
{more, Data, Req} ->
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
io:format("~s", [Data]),
|
2016-08-25 17:40:37 +02:00
|
|
|
read_body_to_console(Req)
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
end.
|
2016-01-14 13:35:25 +01:00
|
|
|
----
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
The `length` and `period` options can also be used.
|
|
|
|
They need to be passed for every call.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
=== Reading a form urlencoded body
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
Cowboy provides a convenient function for reading and
|
|
|
|
parsing bodies sent as application/x-www-form-urlencoded.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2016-08-25 17:40:37 +02:00
|
|
|
{ok, KeyValues, Req} = cowboy_req:read_urlencoded_body(Req0).
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
This function returns a list of key/values, exactly like
|
|
|
|
the function `cowboy_req:parse_qs/1`.
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-08-25 17:40:37 +02:00
|
|
|
The defaults for this function are different. Cowboy will
|
|
|
|
read for up to 64KB and up to 5 seconds. They can be modified:
|
2013-09-23 15:44:10 +02:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
[source,erlang]
|
2016-08-25 17:40:37 +02:00
|
|
|
----
|
|
|
|
{ok, KeyValues, Req} = cowboy_req:read_urlencoded_body(Req0,
|
2016-09-14 18:51:11 +02:00
|
|
|
#{length => 4096, period => 3000}).
|
2016-08-25 17:40:37 +02:00
|
|
|
----
|