cowboy_req.set_resp_body.asciidoc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. = cowboy_req:set_resp_body(3)
  2. == Name
  3. cowboy_req:set_resp_body - Set the response body
  4. == Description
  5. [source,erlang]
  6. ----
  7. set_resp_body(Body, Req :: cowboy_req:req())
  8. -> Req
  9. Body :: cowboy_req:resp_body()
  10. ----
  11. Set the response body.
  12. The response body will be sent when a reply is initiated.
  13. Note that the functions `stream_reply/2,3` and `reply/4`
  14. will override the body set by this function.
  15. This function can also be used to remove a response body
  16. that was set previously. To do so, simply call this function
  17. with an empty body.
  18. == Arguments
  19. Body::
  20. The body can be either a binary value, an iolist or a
  21. `sendfile` tuple telling Cowboy to send the contents of
  22. a file.
  23. Req::
  24. The Req object.
  25. == Return value
  26. A new Req object is returned.
  27. The returned Req object must be used from that point onward,
  28. otherwise the body will not be sent in the response.
  29. == Changelog
  30. * *2.0*: The function now accepts a `sendfile` tuple.
  31. * *2.0*: The `set_resp_body_fun/2,3` functions were removed.
  32. * *1.0*: Function introduced.
  33. == Examples
  34. .Set the response body
  35. [source,erlang]
  36. ----
  37. Req = cowboy_req:set_resp_body(<<"Hello world!">>, Req0).
  38. ----
  39. .Set the response body as an iolist
  40. [source,erlang]
  41. ----
  42. Req = cowboy_req:set_resp_body([
  43. "<html><head><title>",
  44. page_title(),
  45. "</title></head><body>",
  46. page_body(),
  47. "</body></html>"
  48. ], Req0).
  49. ----
  50. .Tell Cowboy to send data from a file
  51. [source,erlang]
  52. ----
  53. {ok, #file_info{size=Size}} = file:read_file_info(Filename),
  54. Req = cowboy_req:set_resp_body({sendfile, 0, Size, Filename}, Req0).
  55. ----
  56. .Clear any previously set response body
  57. [source,erlang]
  58. ----
  59. Req = cowboy_req:set_resp_body(<<>>, Req0).
  60. ----
  61. == See also
  62. link:man:cowboy_req(3)[cowboy_req(3)],
  63. link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)],
  64. link:man:cowboy_req:reply(3)[cowboy_req:reply(3)],
  65. link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)]