aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/cowboy_req.reply.asciidoc
blob: 35403f8ee9ae736e351fe2bdde0ff17ed0ccd5bc (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
= cowboy_req:reply(3)

== Name

cowboy_req:reply - Send the response

== Description

[source,erlang]
----
reply(Status, Req :: cowboy_req:req())
    -> reply(StatusCode, #{}, Req)

reply(Status, Headers, Req :: cowboy_req:req())
    -> Req

reply(Status, Headers, Body, Req :: cowboy_req:req())
    -> Req

Status  :: cowboy:http_status()
Headers :: cowboy:http_headers()
Body    :: cowboy_req:resp_body()
----

Send the response.

The header names must be given as lowercase binary strings.
While header names are case insensitive, Cowboy requires them
to be given as lowercase to function properly.

Cowboy does not allow duplicate header names. Headers set
by this function may overwrite those set by `set_resp_header/3`.

Use link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)]
instead of this function to set cookies.

The `reply/2,3` functions will send the body set previously,
if any. The `reply/4` function always sends the given body,
overriding any previously set.

You do not need to set the content-length header when
sending a response body. Cowboy takes care of it automatically.
You should however provide a content-type header.

No further data can be transmitted after this function
returns. This includes the push mechanism. Attempting to
send two replies, or to push resources after a reply has
been sent, will result in an error.

== Arguments

Status::

The status code for the response.

Headers::

The response headers.

Header names must be given as lowercase binary strings.

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 should be used from that point onward
as it contains updated information about the state of the request.

== Changelog

* *2.0*: Only the Req is returned, it is no longer wrapped in a tuple.
* *1.0*: Function introduced.

== Examples

.Reply
[source,erlang]
----
Req = cowboy_req:reply(404, Req0).
----

.Reply with custom headers
[source,erlang]
----
Req = cowboy_req:reply(401, #{
    <<"www-authenticate">> => <<"Basic realm=\"erlang.org\"">>
}, Req0).
----

.Reply with custom headers and a body
[source,erlang]
----
Req = cowboy_req:reply(200, #{
    <<"content-type">> => <<"text/plain">>
}, "Hello world!", Req0).
----

== See also

link:man:cowboy_req(3)[cowboy_req(3)],
link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)],
link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)],
link:man:cowboy_req:set_resp_body(3)[cowboy_req:set_resp_body(3)],
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)],
link:man:cowboy_req:push(3)[cowboy_req:push(3)]