Issues with Making HTTP Requests Using reqwest Inside task::spawn with Tokio

Hello everyone,

I'm encountering an issue when trying to make HTTP requests using the reqwest library inside an asynchronous task (task::spawn) with Tokio. The request works correctly when executed outside of the task, but inside the task, I receive the following error:

Error executing watch_exploit: error sending request for url (https://api.ransomware.live/recentvictims)

Here's a summary of my code:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    //Workers Secundarios
    task::spawn(async move {
        match watch_exploit::run().await {
            Ok(_) => println!("Watch exploit executado com sucesso"),
            Err(e) => eprintln!("Erro ao executar watch_exploit: {}", e),
        }
    });
    

    Ok(())
}

watch_exploit.rs:


pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
    let client = reqwest::Client::builder()
        .danger_accept_invalid_certs(true)
        .timeout(std::time::Duration::from_secs(30))
        .build()?;

    println!("Iniciando requisição para a API...");
    let res = client
        .get("https://api.ransomware.live/recentvictims")
        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
        .header("Accept", "*/*")
        .header("Connection", "keep-alive")
        .send()
        .await?;

    let body = res.text().await?;
    println!("Dados recebidos: {}", body);

    Ok(())
}

I have verified the machine's connectivity, and access to the URL is working.

Has anyone experienced something similar or have any ideas on what might be causing this issue? Any help would be greatly appreciated!

The tokio task returns a JoinHandle which can be awaited such that the main thread does not exit before the inner task is finished, which would cancel the spawned tasks before completion (tokio::task module docs).

Thanks man!