reqwest v0.2
The latest version of reqwest includes a bag of goodies, but the highlights are Response.json()
and RedirectPolicy
.
The first allows you to easily decode a JSON body into some other type. This type only needs to implement Deserialize
, so you can use customs structs, but as you can see in the example, simple things work straight out of the box, like a HashMap
.
let res = reqwest::get("http://httpbin.org/user-agent").unwrap();
let body: HashMap<String, String> = res.json().unwrap();
assert_eq!(body["user-agent"], "reqwest/0.2.0");
The second highlight, RedirectPolicy
, improves on the handling of redirects in the default case, and allows you to create a powerful custom policy easily.
let blocklist = std::env::var("BLOCKLIST").unwrap()
.split(",")
.collect::<Vec<_>>();
let mut client = reqwest::Client::new().unwrap();
client.redirect(reqwest::RedirectPolicy::custom(move |next, previous| {
if blocklist.contains(&next.host_str().unwrap()) {
Ok(false)
} else if previous.len() == 3 {
Err(reqwest::Error::TooManyRedirects)
} else {
Ok(true)
}
})