cowboy_req.stream_body.asciidoc 1.6 KB

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