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.
Options — http(method, url, #{ … }):
| Field | Type | Description |
|---|---|---|
headers | map | request headers, e.g. #{ "Content-Type": "application/json" } |
body | string or map | request 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.