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