#http #https #client #request

minreq

Simple, minimal-dependency HTTP client

9 releases (stable)

✓ Uses Rust 2018 edition

new 1.1.2 Apr 14, 2019
1.1.1 Mar 28, 2019
1.0.3 Jul 30, 2018
1.0.2 Mar 18, 2018
0.1.2 Mar 18, 2018

#28 in HTTP client

Download history 5/week @ 2018-12-30 15/week @ 2019-01-06 7/week @ 2019-01-13 39/week @ 2019-01-20 23/week @ 2019-01-27 27/week @ 2019-02-03 33/week @ 2019-02-10 46/week @ 2019-02-17 49/week @ 2019-02-24 76/week @ 2019-03-03 119/week @ 2019-03-10 83/week @ 2019-03-17 265/week @ 2019-03-24 83/week @ 2019-03-31 96/week @ 2019-04-07

319 downloads per month
Used in 2 crates (1 directly)

ISC license

22KB
383 lines

minreq

Crates.io Documentation CI

Simple, minimal-dependency HTTP client.

Documentation

License

This crate is distributed under the terms of the ISC license.


lib.rs:

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: 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 call with_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 called foo that uses minreq, and you want all the requests made by that program to timeout in 8 seconds, you launch the program like so:
    $ MINREQ_TIMEOUT=8 ./foo
    
    Or add the following somewhere before the requests in the code.
    use std::env;
    
    env::set_var("MINREQ_TIMEOUT", "8");
    

Dependencies

~1.5MB
~39K SLoC