[−][src]Crate minreq
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.
Additional features
Since the crate is supposed to be minimal in terms of
dependencies, optional functionality can be enabled by specifying
features for minreq
dependency in Cargo.toml
:
[dependencies]
minreq = { version = "*", features = ["https", "json-using-serde"] }
Below is the list of all available features.
https
This feature uses the (very good) rustls
crate to secure connection.
json-using-serde
This feature allows both serialize and deserialize JSON payload using
serde_json
crate.
Request
and Response
expose with_json()
and json()
respectively
for converting struct to JSON and back.
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: There is no timeout by default. if let Ok(response) = minreq::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:minreq::get("/").with_timeout(8).send();
- Set the environment variable
MINREQ_TIMEOUT
to the desired amount of seconds until timeout. Ie. if you have a program calledfoo
that 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 ./foo
use 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 create_request with |
create_request | Sends a request to |
delete | Alias for create_request with |
get | Alias for create_request with |
head | Alias for create_request with |
options | Alias for create_request with |
patch | Alias for create_request with |
post | Alias for create_request with |
put | Alias for create_request with |
trace | Alias for create_request with |
Type Definitions
URL | A URL type for requests. |