How can I accept invalid or self-signed SSL certificates in Rust futures reqwest?

Viewed 1721

My code looks like the following:

let fetches = futures::stream::iter(
    hosts.into_iter().map(|url| {
        async move {
                match reqwest::get(&url).await {
                    // Ok and Err statements here!
                }

But, the problem here is that it gives an error for URLs with invalid or self-signed SSL certificate. So, I tried to do the following:

let fetches = futures::stream::iter(
    hosts.into_iter().map(|url| {
        async move {
            match reqwest::Client::builder().danger_accept_invalid_certs(true).build().unwrap().get(&url).await {
                // Ok and Err statements here!
            }

When I try to build it with Cargo, it says "error[E0277]: `RequestBuilder` is not a future".

So, how can I make my code accept invalid certificates?

1 Answers

Unlike the top-level get() function, which returns a Response, the Client::get() method which you call in second code, returns a RequestBuilder, which you must send() to actually communicate.

Adding the missing send() allows the code to compile (playgropund):

fn main() {
    let hosts: Vec<String> = vec![];
    let fetches = futures::stream::iter(hosts.into_iter().map(|url| async move {
        match reqwest::Client::builder()
            .danger_accept_invalid_certs(true)
            .build()
            .unwrap()
            .get(&url)
            .send()
            .await
        {
            Ok(x) => x,
            Err(x) => panic!(),
        }
    }));
}
Related