Lyricdev
GitHub

class Response

pub class Response

A response: the status, the headers, the body as a Reader that ends where the message does. A handler makes one with Response.of, Response.withText, Response.withJson, Response.stream or Response.empty.

Fields

pub var status: Status
pub var headers: Headers
pub var body: Body

Methods

pub static fn of(
    status: int,
    body: Slice<uint8> = [],
    contentType: string = "application/octet-stream"
): Response

Returns a response of the bytes, their length known; Content-Type is set where there are any.

pub static fn withText(
    text: string,
    status: int = 200,
    contentType: string = "text/plain; charset=utf-8"
): Response

Returns a response of the text, UTF-8.

pub static fn withJson<T :: [Encode]>(
    value: T,
    status: int = 200
): Response throws EncodeError

Returns a response of the value as JSON, Content-Type: application/json.

Errors

Throws EncodeError where the value cannot be encoded as JSON.

pub static fn stream(
    status: int,
    source: Reader,
    length: ?int = null,
    contentType: string = "application/octet-stream"
): Response

Returns a response whose body is read from source as it is written — in chunks where length is null, with Content-Length otherwise.

pub static fn empty(status: int): Response

Returns a response without a body.

pub fn bytes(): uint8[] throws [IoError, Cancelled]

Reads the body whole.

Errors

Throws IoError where the connection fails or the body is framed wrongly, and Cancelled where the task is cancelled while reading.

pub fn text(): string throws [IoError, Cancelled]

Reads the body whole as text (UTF-8, a wrong byte U+FFFD).

Errors

Throws as bytes does.

pub fn json<T :: [Decode]>(): T throws [JsonError, DecodeError, IoError, Cancelled]

Reads the body whole and decodes it as JSON into T.

Errors

Throws JsonError for a body that is no JSON, DecodeError where it does not fit T, and IoError or Cancelled as bytes does.