Crate minreq [−] [src]
Minreq
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:
minreq = { version = "1.0.0", features = ["https"] }
Examples
Get
// This is a simple example of sending a GET request and // printing out the response. if let Ok(response) = minreq::get("http://httpbin.org/ip").send() { println!("{}", response.body); }
Body
// To include a body, add .with_body("") before .send(). if let Ok(response) = minreq::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) = minreq::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: The default timeout is 5 seconds. if let Ok(response) = minreq::post("http://httpbin.org/delay/6") .with_timeout(10) .send() { println!("{}", response.body); }
Timeouts
The timeout of the created request is 5 seconds by default. You can change this in two ways:
- Use this function (
create_request) and callwith_timeouton it to set the timeout per-request like so:minreq::get("/").with_timeout(8).send();
- Set the environment variable
MINREQ_TIMEOUTto the desired amount of seconds until timeout. Ie. if you have a program calledfoothat uses minreq, 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.$ MINREQ_TIMEOUT=8 ./foouse std::env; env::set_var("MINREQ_TIMEOUT", "8");
Structs
| Request |
An HTTP request. |
| Response |
An HTTP response. |
Enums
| Method |
An HTTP request method. |
Functions
| connect |
Alias for send with |
| create_request |
Sends a request to |
| delete |
Alias for send with |
| get |
Alias for send with |
| head |
Alias for send with |
| options |
Alias for send with |
| patch |
Alias for send with |
| post |
Alias for send with |
| put |
Alias for send with |
| trace |
Alias for send with |
Type Definitions
| URL |
A URL type for requests. |