Lyricdev
GitHub

struct Request

pub struct Request

A request: the method, the URI, the headers, and a body — a Reader with its length where known (Content-Length), sent in chunks otherwise.

Fields

pub var method: Method = Method.Get
pub var uri: Uri
pub var headers: Headers = Headers.new()
pub var body: ?Reader = null
pub var contentLength: ?int = null

Methods

pub static fn get(uri: Uri): Request
pub static fn post(
    uri: Uri,
    body: Slice<uint8>,
    contentType: string = "application/octet-stream"
): Request

Returns a POST of the bytes, their length known.

pub mut fn setBody(bytes: Slice<uint8>): void

Sets the bytes as the body, Content-Length their count.

pub mut fn setText(text: string, contentType: string = "text/plain; charset=utf-8"): void

Sets the text as the body, UTF-8, and the Content-Type.

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

Reads the body whole (a server's handler reads the request it was given this way); empty where there is none.

Errors

Throws IoError where reading the body fails, 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.