xur_

> module

std::http

HTTP client

use std::http; feature http native only

std::http is a blocking HTTP client built on ureq. Requests return Result<Response, str>: Err holds the message for a network failure, a timeout, or a status code of 400 or above (such as "HTTP error: status 404"). Redirects are followed. Each request times out after 30 seconds unless the builder sets otherwise.

> functions

# delete(url: str) -> Result<Response, str>

Sends a GET, DELETE or HEAD request.

# get(url: str) -> Result<Response, str>

Sends a GET, DELETE or HEAD request.

# patch(url: str, body?: str | bytes) -> Result<Response, str>

Sends a POST, PUT or PATCH request. body is optional; without it the body is empty.

# post(url: str, body?: str | bytes) -> Result<Response, str>

Sends a POST, PUT or PATCH request. body is optional; without it the body is empty.

# put(url: str, body?: str | bytes) -> Result<Response, str>

Sends a POST, PUT or PATCH request. body is optional; without it the body is empty.

# request(method: str, url: str) -> RequestBuilder

A builder for a request with headers, a query, a body or a timeout. method is case-insensitive. A request with a body is sent as PUT or PATCH if that is the method and as POST otherwise; one without a body is sent as DELETE, HEAD, OPTIONS, POST, PUT or PATCH if named, and otherwise as GET.

> types

┌ type RequestBuilder

Each method except send returns a new builder; the original is unchanged.

# request_builder.basic_auth(user: str, pass: str) -> RequestBuilder

Adds an Authorization: Basic header with user:pass in base64.

# request_builder.bearer_auth(token: str) -> RequestBuilder

Adds an Authorization: Bearer token header.

# request_builder.body(body: str | bytes) -> RequestBuilder

Sets the request body.

# request_builder.header(name: str, value: str) -> RequestBuilder

Adds a header.

# request_builder.json_body(value: any) -> RequestBuilder

Sets the body to value as JSON and the Content-Type to application/json.

# request_builder.query(key: str, value: str) -> RequestBuilder

Adds a URL query parameter.

# request_builder.send() -> Result<Response, str>

Sends the request.

# request_builder.timeout(secs: u64) -> RequestBuilder

Sets the timeout for the whole request, in seconds (30 by default).

┌ type Response
# response.bytes() -> bytes

The body as raw bytes.

# response.header(name: str) -> Option<str>

The first header called name, ignoring case.

# response.headers() -> record

All headers as a record from name to value.

# response.json() -> Result<any, str>

The body parsed as JSON, as by json::parse.

# response.status() -> i64

The HTTP status code.

# response.status_text() -> str

The standard reason phrase for the status, such as "OK" or "Not Found".

# response.text() -> str

The body as text. Invalid UTF-8 is replaced with U+FFFD.

# response.url() -> str

The URL of the response, after any redirects.

> examples

use std::http;
use std::io;

fn main() -> Result<()> {
    let resp = http::get("https://httpbin.org/get")?;
    io::println("status: {}", resp.status());
    io::println("body: {}", resp.text());
    Ok(())
}
use std::http;
use std::io;

fn main() -> Result<()> {
    let data = @{ name: "xur" };
    let resp = http::request("POST", "https://httpbin.org/post")
        .bearer_auth("token")
        .json_body(data)
        .send()?;
    io::println("{}", resp.json()?.data);
    Ok(())
}