rustrust-cargorust-tokioreqwest

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


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?


Solution

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

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

    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!(),
            }
        }));
    }