> module
std::http
HTTP client
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
Sends a POST, PUT or PATCH request. body is optional; without it the body is empty.
Sends a POST, PUT or PATCH request. body is optional; without it the body is empty.
Sends a POST, PUT or PATCH request. body is optional; without it the body is empty.
request(method: str, url: str) -> RequestBuilderA 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
Each method except send returns a new builder; the original is unchanged.
request_builder.basic_auth(user: str, pass: str) -> RequestBuilderAdds an Authorization: Basic header with user:pass in base64.
request_builder.bearer_auth(token: str) -> RequestBuilderAdds an Authorization: Bearer token header.
request_builder.body(body: str | bytes) -> RequestBuilderSets the request body.
request_builder.header(name: str, value: str) -> RequestBuilderAdds a header.
request_builder.json_body(value: any) -> RequestBuilderSets the body to value as JSON and the Content-Type to application/json.
request_builder.query(key: str, value: str) -> RequestBuilderAdds a URL query parameter.
request_builder.timeout(secs: u64) -> RequestBuilderSets the timeout for the whole request, in seconds (30 by default).
The standard reason phrase for the status, such as "OK" or "Not Found".
> 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(())
}