data Reply
The answer of a handler.
Example:
use std.http { Reply }
fn main() io {
let r = Reply.json("{{\"ok\":true}}", 201).withHeader("Location", "/items/1")
print("{r.status} {r.headers.len()}")
}Output
201 2
pub(api) data ReplyThread safety. a plain value built by the handler.
Since. 1.0.0-beta
Fields
pub(api) status: i64 = 200 | Status code. |
pub(api) headers: List<Header> = [] | Header fields to send. |
pub(api) body: bytes = Utf8.encode("") | The body. |
pub(api) producer: (fn(BodyWriter) -> () | NetError net, io, time)? = none | A streamed body ( |
pub(api) socket: (fn(WebSocket) -> () | NetError net, io, time)? = none | A WebSocket upgrade ( |
pub(api) subprotocols: str = "" | WebSocket: subprotocols offered, comma separated. |
pub(api) compress: bool = true | WebSocket: offer permessage-deflate. |
Methods
text | (body: str, status: i64) -> ReplyA text/plain; charset=utf-8 reply. |
html | (body: str, status: i64) -> ReplyA text/html; charset=utf-8 reply. |
json | (body: str, status: i64) -> ReplyA body that is already JSON text. |
bytes | (body: bytes, contentType: str, status: i64) -> ReplyA reply with a binary body and the given Content-Type. |
empty | (status: i64) -> ReplyA reply with no body (default 204 No Content). |
redirect | (location: str, status: i64) -> ReplyA redirect to location (default 302 Found; use 301, 303, 307 or 308 as needed). |
stream | (contentType: str, producer: fn(BodyWriter) -> () | NetError net, io, time escaping, status: i64) -> ReplyA streamed body of unknown length (chunked); producer writes the pieces. |
sse | (producer: fn(SseWriter) -> () | NetError net, io, time escaping) -> ReplyA server-sent events stream (text/event-stream); producer sends events until it returns or the client leaves. |
websocket | (body: fn(WebSocket) -> () | NetError net, io, time escaping, protocols: str) -> ReplyUpgrades the connection to a WebSocket and runs body with it; the socket is closed when body returns. |
withStatus | (code: i64) -> ReplyA copy of the reply with another status code. |
withHeader | (name: str, value: str) -> ReplyA copy of the reply with one more header (headers are added, not replaced). |
Defined in: std/http/http.oxl:2646