Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

HTTP

Constructor

http(method: string, url: string)

Returns HttpResponse

Make an HTTP request and return the response.

Example

#![allow(unused)]
fn main() {
let res = http("GET", env("API_URL") + "/calls");
res.expect_status(200);
}

http(method: string, url: string, options: map)

Returns HttpResponse

Make an HTTP request with options and return the response.

Optionshttp(method, url, #{ … }):

FieldTypeDescription
headersmaprequest headers, e.g. #{ "Content-Type": "application/json" }
bodystring or maprequest body; a map is encoded to JSON

Example

#![allow(unused)]
fn main() {
let res = http("POST", env("API_URL") + "/calls", #{
    headers: #{ "Content-Type": "application/json" },
    body: #{ to: "+49301234567" },
});
}

Methods

resp.expect_status(code: int)

Receiver HttpResponse

Assert and report the status; errors on mismatch.

resp.header(name: string)

Receiver HttpResponse · Returns any

A response header value (string), or () if absent.

resp.json()

Receiver HttpResponse · Returns any

The whole JSON body as a native value (object→map, array, …).

resp.json(path: string)

Receiver HttpResponse · Returns any

The value at a dotted JSON path (e.g. "data.id"), typed: object→map, array, number, bool, null(). Errors if the path is missing.

Example

#![allow(unused)]
fn main() {
assert(res.json("data.id")).equals(42);
}

Fields

resp.body

Receiver HttpResponse · Returns string

The HTTP response body as a string.

resp.status

Receiver HttpResponse · Returns int

The HTTP response status code.