OxL Справочник OxL
РусскийEnglish

data Reply

The answer of a handler.

Example:

oxl✓ проверяется в CI
use std.http { Reply }

fn main() io {
  let r = Reply.json("{{\"ok\":true}}", 201).withHeader("Location", "/items/1")
  print("{r.status} {r.headers.len()}")
}
Вывод
201 2
pub(api) data Reply

Потокобезопасность. a plain value built by the handler.

С версии. 1.0.0-beta

Поля

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 (Reply.stream, Reply.sse): runs after the head is sent.

pub(api) socket: (fn(WebSocket) -> () | NetError net, io, time)? = none

A WebSocket upgrade (Reply.websocket).

pub(api) subprotocols: str = ""

WebSocket: subprotocols offered, comma separated.

pub(api) compress: bool = true

WebSocket: offer permessage-deflate.

Методы

text(body: str, status: i64) -> Reply
A text/plain; charset=utf-8 reply.
html(body: str, status: i64) -> Reply
A text/html; charset=utf-8 reply.
json(body: str, status: i64) -> Reply
A body that is already JSON text.
bytes(body: bytes, contentType: str, status: i64) -> Reply
A reply with a binary body and the given Content-Type.
empty(status: i64) -> Reply
A reply with no body (default 204 No Content).
redirect(location: str, status: i64) -> Reply
A 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) -> Reply
A streamed body of unknown length (chunked); producer writes the pieces.
sse(producer: fn(SseWriter) -> () | NetError net, io, time escaping) -> Reply
A 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) -> Reply
Upgrades the connection to a WebSocket and runs body with it; the socket is closed when body returns.
withStatus(code: i64) -> Reply
A copy of the reply with another status code.
withHeader(name: str, value: str) -> Reply
A copy of the reply with one more header (headers are added, not replaced).

Определено в: std/http/http.oxl:2646

См. также