| 1 |
= cowboy_req:reply(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:reply - Send the response |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
reply(Status, Req :: cowboy_req:req()) |
| 12 |
-> reply(StatusCode, #{}, Req) |
| 13 |
|
| 14 |
reply(Status, Headers, Req :: cowboy_req:req()) |
| 15 |
-> Req |
| 16 |
|
| 17 |
reply(Status, Headers, Body, Req :: cowboy_req:req()) |
| 18 |
-> Req |
| 19 |
|
| 20 |
Status :: cowboy:http_status() |
| 21 |
Headers :: cowboy:http_headers() |
| 22 |
Body :: cowboy_req:resp_body() |
| 23 |
---- |
| 24 |
|
| 25 |
Send the response. |
| 26 |
|
| 27 |
The header names must be given as lowercase binary strings. |
| 28 |
While header names are case insensitive, Cowboy requires them |
| 29 |
to be given as lowercase to function properly. |
| 30 |
|
| 31 |
Cowboy does not allow duplicate header names. Headers set |
| 32 |
by this function may overwrite those set by `set_resp_header/3` |
| 33 |
and `set_resp_headers/2`. |
| 34 |
|
| 35 |
Use link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)] |
| 36 |
instead of this function to set cookies. |
| 37 |
|
| 38 |
The `reply/2,3` functions will send the body set previously, |
| 39 |
if any. The `reply/4` function always sends the given body, |
| 40 |
overriding any previously set. |
| 41 |
|
| 42 |
You do not need to set the content-length header when |
| 43 |
sending a response body. Cowboy takes care of it automatically. |
| 44 |
You should however provide a content-type header. |
| 45 |
|
| 46 |
No further data can be transmitted after this function |
| 47 |
returns. This includes the push mechanism. Attempting to |
| 48 |
send two replies, or to push resources after a reply has |
| 49 |
been sent, will result in an error. |
| 50 |
|
| 51 |
== Arguments |
| 52 |
|
| 53 |
Status:: |
| 54 |
|
| 55 |
The status code for the response. |
| 56 |
|
| 57 |
Headers:: |
| 58 |
|
| 59 |
The response headers. |
| 60 |
+ |
| 61 |
Header names must be given as lowercase binary strings. |
| 62 |
|
| 63 |
Body:: |
| 64 |
|
| 65 |
The body can be either a binary value, an iolist or a |
| 66 |
`sendfile` tuple telling Cowboy to send the contents of |
| 67 |
a file. |
| 68 |
|
| 69 |
Req:: |
| 70 |
|
| 71 |
The Req object. |
| 72 |
|
| 73 |
== Return value |
| 74 |
|
| 75 |
A new Req object is returned. |
| 76 |
|
| 77 |
The returned Req object should be used from that point onward |
| 78 |
as it contains updated information about the state of the request. |
| 79 |
|
| 80 |
== Changelog |
| 81 |
|
| 82 |
* *2.0*: Only the Req is returned, it is no longer wrapped in a tuple. |
| 83 |
* *1.0*: Function introduced. |
| 84 |
|
| 85 |
== Examples |
| 86 |
|
| 87 |
.Reply |
| 88 |
[source,erlang] |
| 89 |
---- |
| 90 |
Req = cowboy_req:reply(404, Req0). |
| 91 |
---- |
| 92 |
|
| 93 |
.Reply with custom headers |
| 94 |
[source,erlang] |
| 95 |
---- |
| 96 |
Req = cowboy_req:reply(401, #{ |
| 97 |
<<"www-authenticate">> => <<"Basic realm=\"erlang.org\"">> |
| 98 |
}, Req0). |
| 99 |
---- |
| 100 |
|
| 101 |
.Reply with custom headers and a body |
| 102 |
[source,erlang] |
| 103 |
---- |
| 104 |
Req = cowboy_req:reply(200, #{ |
| 105 |
<<"content-type">> => <<"text/plain">> |
| 106 |
}, "Hello world!", Req0). |
| 107 |
---- |
| 108 |
|
| 109 |
== See also |
| 110 |
|
| 111 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 112 |
link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)], |
| 113 |
link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)], |
| 114 |
link:man:cowboy_req:set_resp_headers(3)[cowboy_req:set_resp_headers(3)], |
| 115 |
link:man:cowboy_req:set_resp_body(3)[cowboy_req:set_resp_body(3)], |
| 116 |
link:man:cowboy_req:inform(3)[cowboy_req:inform(3)], |
| 117 |
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)], |
| 118 |
link:man:cowboy_req:push(3)[cowboy_req:push(3)] |