0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00

Document body reading in auto mode

It is now tested both via cowboy_req:read_body and
via cowboy_req:cast.

Removes a bad example from the guide of body reading
with period of infinity, which does not work.
This commit is contained in:
Loïc Hoguin 2024-01-08 15:13:18 +01:00
parent c1490d7d55
commit e4a78aaeb1
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
8 changed files with 137 additions and 21 deletions

View file

@ -74,18 +74,34 @@ only up to 1MB for up to 5 seconds:
#{length => 1000000, period => 5000}).
----
You may also disable the length limit:
[source,erlang]
{ok, Data, Req} = cowboy_req:read_body(Req0, #{length => infinity}).
This makes the function wait 15 seconds and return with
whatever arrived during that period. This is not
recommended for public facing applications.
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:
[source,erlang]
----
Ref = make_ref(),
cowboy_req:cast({read_body, self(), Ref, auto, infinity}, Req).
----
Cowboy will wait indefinitely for data and then send a
`request_body` message as soon as it has data available,
regardless of length.
[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.
=== Streaming the body
When the body is too large, the first call will return