| 1 |
= cowboy_req:stream_reply(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:stream_reply - Send the response headers |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
stream_reply(Status, Req :: cowboy_req:req()) |
| 12 |
-> stream_reply(StatusCode, #{}, Req) |
| 13 |
|
| 14 |
stream_reply(Status, Headers, Req :: cowboy_req:req()) |
| 15 |
-> Req |
| 16 |
|
| 17 |
Status :: cowboy:http_status() |
| 18 |
Headers :: cowboy:http_headers() |
| 19 |
---- |
| 20 |
|
| 21 |
Send the response headers. |
| 22 |
|
| 23 |
The header names must be given as lowercase binary strings. |
| 24 |
While header names are case insensitive, Cowboy requires them |
| 25 |
to be given as lowercase to function properly. |
| 26 |
|
| 27 |
Cowboy does not allow duplicate header names. Headers set |
| 28 |
by this function may overwrite those set by `set_resp_header/3`. |
| 29 |
|
| 30 |
Use link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)] |
| 31 |
instead of this function to set cookies. |
| 32 |
|
| 33 |
If a response body was set before calling this function, |
| 34 |
it will not be sent. |
| 35 |
|
| 36 |
Use link:man:cowboy_req:stream_body(3)[cowboy_req:stream_body(3)] |
| 37 |
to stream the response body and optionally |
| 38 |
link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] |
| 39 |
to send response trailer field values. |
| 40 |
|
| 41 |
You may want to set the content-length header when using |
| 42 |
this function, if it is known in advance. This will allow |
| 43 |
clients using HTTP/2 and HTTP/1.0 to process the response |
| 44 |
more efficiently. |
| 45 |
|
| 46 |
The streaming method varies depending on the protocol being |
| 47 |
used. HTTP/2 will use the usual DATA frames. HTTP/1.1 will |
| 48 |
use chunked transfer-encoding, if the content-length |
| 49 |
response header is set the body will be sent without chunked |
| 50 |
chunked transfer-encoding. HTTP/1.0 will send the body |
| 51 |
unmodified and close the connection at the end if no |
| 52 |
content-length was set. |
| 53 |
|
| 54 |
It is not possible to push resources after this function |
| 55 |
returns. Any attempt will result in an error. |
| 56 |
|
| 57 |
== Arguments |
| 58 |
|
| 59 |
Status:: |
| 60 |
|
| 61 |
The status code for the response. |
| 62 |
|
| 63 |
Headers:: |
| 64 |
|
| 65 |
The response headers. |
| 66 |
+ |
| 67 |
Header names must be given as lowercase binary strings. |
| 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 must be used from that point onward |
| 78 |
in order to be able to stream the response body. |
| 79 |
|
| 80 |
== Changelog |
| 81 |
|
| 82 |
* *2.0*: Only the Req is returned, it is no longer wrapped in a tuple. |
| 83 |
* *2.0*: Function introduced. Replaces `chunked_reply/1,2`. |
| 84 |
|
| 85 |
== Examples |
| 86 |
|
| 87 |
.Initiate the response |
| 88 |
[source,erlang] |
| 89 |
---- |
| 90 |
Req = cowboy_req:stream_reply(200, Req0). |
| 91 |
---- |
| 92 |
|
| 93 |
.Stream the response with custom headers |
| 94 |
[source,erlang] |
| 95 |
---- |
| 96 |
Req = cowboy_req:stream_reply(200, #{ |
| 97 |
<<"content-type">> => <<"text/plain">> |
| 98 |
}, Req0), |
| 99 |
cowboy_req:stream_body(<<"Hello\n">>, nofin, Req), |
| 100 |
timer:sleep(1000), |
| 101 |
cowboy_req:stream_body(<<"World!\n">>, fin, Req). |
| 102 |
---- |
| 103 |
|
| 104 |
== See also |
| 105 |
|
| 106 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 107 |
link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)], |
| 108 |
link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)], |
| 109 |
link:man:cowboy_req:set_resp_headers(3)[cowboy_req:set_resp_headers(3)], |
| 110 |
link:man:cowboy_req:inform(3)[cowboy_req:inform(3)], |
| 111 |
link:man:cowboy_req:reply(3)[cowboy_req:reply(3)], |
| 112 |
link:man:cowboy_req:stream_body(3)[cowboy_req:stream_body(3)], |
| 113 |
link:man:cowboy_req:stream_events(3)[cowboy_req:stream_events(3)], |
| 114 |
link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)], |
| 115 |
link:man:cowboy_req:push(3)[cowboy_req:push(3)] |