I have a situation where I want to read some headers via reqwest::header::HeaderValue
I am doing so using
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let url = "https://httpbin.org/get";
let resp = reqwest::Client::new().get(url).send().await?;
// read response headers
// default if the header does not exist
let resp_headers: &str = resp
.headers()
.get("content-length")
.unwrap_or(&reqwest::header::HeaderValue::from_str("100").unwrap())
.to_str()
.unwrap_or("100");
println!("{}", resp_headers);
Ok(())
}
This doesn’t seem idiomatic and won’t compile. Looking for how to approach a scenario like this where there are several necessary unwraps to process and cast a type into desired end result.