| 1 |
= cowboy_req:stream_body(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:stream_body - Stream the response body |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
stream_body(Data, IsFin, Req :: cowboy_req:req()) -> ok |
| 12 |
|
| 13 |
Data :: cowboy_req:resp_body() |
| 14 |
IsFin :: fin | nofin |
| 15 |
---- |
| 16 |
|
| 17 |
Stream the response body. |
| 18 |
|
| 19 |
This function may be called as many times as needed after |
| 20 |
initiating a response using the |
| 21 |
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)] |
| 22 |
function. |
| 23 |
|
| 24 |
The second argument indicates if this call is the final |
| 25 |
call. Use the `nofin` value until you know no more data |
| 26 |
will be sent. The final call should use `fin` (possibly |
| 27 |
with an empty data value) or be a call to the |
| 28 |
link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] |
| 29 |
function. |
| 30 |
|
| 31 |
Note that not using `fin` for the final call is not an |
| 32 |
error; Cowboy will take care of it when the request |
| 33 |
handler terminates if needed. Depending on the resource |
| 34 |
it may however be more efficient to do it as early as |
| 35 |
possible. |
| 36 |
|
| 37 |
You do not need to handle HEAD requests specifically as |
| 38 |
Cowboy will ensure no data is sent when you call this function. |
| 39 |
|
| 40 |
== Arguments |
| 41 |
|
| 42 |
Data:: |
| 43 |
|
| 44 |
The data to be sent. |
| 45 |
|
| 46 |
IsFin:: |
| 47 |
|
| 48 |
A flag indicating whether this is the final piece of data |
| 49 |
to be sent. |
| 50 |
|
| 51 |
Req:: |
| 52 |
|
| 53 |
The Req object. |
| 54 |
|
| 55 |
== Return value |
| 56 |
|
| 57 |
The atom `ok` is always returned. It can be safely ignored. |
| 58 |
|
| 59 |
== Changelog |
| 60 |
|
| 61 |
* *2.6*: The `Data` argument can now be a sendfile tuple. |
| 62 |
* *2.0*: Function introduced. Replaces `chunk/2`. |
| 63 |
|
| 64 |
== Examples |
| 65 |
|
| 66 |
.Stream the response body |
| 67 |
[source,erlang] |
| 68 |
---- |
| 69 |
Req = cowboy_req:stream_reply(200, #{ |
| 70 |
<<"content-type">> => <<"text/plain">> |
| 71 |
}, Req0), |
| 72 |
cowboy_req:stream_body(<<"Hello\n">>, nofin, Req), |
| 73 |
timer:sleep(1000), |
| 74 |
cowboy_req:stream_body(<<"World!\n">>, fin, Req). |
| 75 |
---- |
| 76 |
|
| 77 |
== See also |
| 78 |
|
| 79 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 80 |
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)], |
| 81 |
link:man:cowboy_req:stream_events(3)[cowboy_req:stream_events(3)], |
| 82 |
link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] |