aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/cowboy_req.set_resp_body.asciidoc
blob: 5537dae0d820aafadb38a32c7b6c02d2a7fecd1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
= cowboy_req:set_resp_body(3)

== Name

cowboy_req:set_resp_body - Set the response body

== Description

[source,erlang]
----
set_resp_body(Body, Req :: cowboy_req:req())
	-> Req

Body :: cowboy_req:resp_body()
----

Set the response body.

The response body will be sent when a reply is initiated.
Note that the functions `stream_reply/2,3` and `reply/4`
will override the body set by this function.

This function can also be used to remove a response body
that was set previously. To do so, simply call this function
with an empty body.

== Arguments

Body::

The body can be either a binary value, an iolist or a
`sendfile` tuple telling Cowboy to send the contents of
a file.

Req::

The Req object.

== Return value

A new Req object is returned.

The returned Req object must be used from that point onward,
otherwise the body will not be sent in the response.

== Changelog

* *2.0*: The function now accepts a `sendfile` tuple.
* *2.0*: The `set_resp_body_fun/2,3` functions were removed.
* *1.0*: Function introduced.

== Examples

.Set the response body
[source,erlang]
----
Req = cowboy_req:set_resp_body(<<"Hello world!">>, Req0).
----

.Set the response body as an iolist
[source,erlang]
----
Req = cowboy_req:set_resp_body([
    "<html><head><title>",
    page_title(),
    "</title></head><body>",
    page_body(),
    "</body></html>"
], Req0).
----

.Tell Cowboy to send data from a file
[source,erlang]
----
{ok, #file_info{size=Size}} = file:read_file_info(Filename),
Req = cowboy_req:set_resp_body({sendfile, 0, Size, Filename}, Req0).
----

.Clear any previously set response body
[source,erlang]
----
Req = cowboy_req:set_resp_body(<<>>, Req0).
----

== See also

link:man:cowboy_req(3)[cowboy_req(3)],
link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)],
link:man:cowboy_req:reply(3)[cowboy_req:reply(3)],
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)]