1 unstable release
0.1.0 | Apr 13, 2019 |
---|
#90 in #https
21KB
375 lines
mrq
Simple, minimal-dependency HTTP client based on minreq
License
This crate is distributed under the terms of the ISC license.
lib.rs
:
mrq
Simple, minimal-dependency HTTP client. The library has a very minimal API, so you'll probably know everything you need to after reading a few examples.
HTTPS
Since the crate is supposed to be minimal in terms of
dependencies, HTTPS is a feature on its own, as it requires the
(very good) rustls
crate. To
be able to send HTTPS requests, you need to change your
Cargo.toml's [dependencies]
part to something like the
following:
mrq = { version = "0.1.0", features = ["https"] }
Examples
Get
// This is a simple example of sending a GET request and
// printing out the response.
if let Ok(response) = mrq::get("http://httpbin.org/ip").send() {
println!("{}", response.body);
}
Body
// To include a body, add .with_body("") before .send().
if let Ok(response) = mrq::post("http://httpbin.org/post")
.with_body("Pong!")
.send()
{
println!("{}", response.body);
}
Headers
// To add a header, add .with_header("Key", "Value") before .send().
if let Ok(response) = mrq::get("http://httpbin.org/headers")
.with_header("Accept", "text/plain")
.with_header("Something", "Interesting")
.send()
{
println!("{}", response.body);
}
Timeouts
// To avoid timing out, or limit the request's response time even more,
// use .with_timeout(n) before .send(). The given value is in seconds.
// NOTE: There is no timeout by default.
if let Ok(response) = mrq::post("http://httpbin.org/delay/6")
.with_timeout(10)
.send()
{
println!("{}", response.body);
}
Timeouts
By default, a request has no timeout. You can change this in two ways:
- Use this function (
create_request
) and callwith_timeout
on it to set the timeout per-request like so:mrq::get("/").with_timeout(8).send();
- Set the environment variable
mrq_TIMEOUT
to the desired amount of seconds until timeout. Ie. if you have a program calledfoo
that uses mrq, and you want all the requests made by that program to timeout in 8 seconds, you launch the program like so:
Or add the following somewhere before the requests in the code.$ mrq_TIMEOUT=8 ./foo
use std::env; env::set_var("mrq_TIMEOUT", "8");
Dependencies
~0–1.7MB
~41K SLoC