Force HTTP request to wait until there is a response

I implemented the following cURL command with reqwest.

curl -X POST --url http://localhost:8001/api/v1/scan --data "hash=30515b7db737547be080f66d5ed0223d" -H "Authorization:<API_KEY>"

[dependencies.reqwest]
version = "0.12"
features = ["json", "blocking"]
//previous code
let mut headers = header::HeaderMap::new();
headers.insert("Authorization", "<API_KEY>".parse().unwrap());
headers.insert("Content-Type", "application/x-www-form-urlencoded".parse().unwrap());

let client = reqwest::blocking::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .unwrap();
let res = client.post([MOBSF_URL ,"/scan"].concat().as_str())
        .timeout(Duration::from_secs(999999999999))
        .headers(headers)
        .body(query)
        .send()
        .unwrap();
if res.status() == 200 {
        info!("Successful scan...");
        Ok(res)
    }

The cURL request waits until I get the response with the JSON I except. But reqwest does not wait until I get the expected JSON. What do I miss here?

For some reason if the timeout is set to default then I get an error. But If I replace the timeout then it works but without a JSON response.

called `Result::unwrap()` on an `Err` value: reqwest::Error 
{ kind: Request, url: Url { scheme: "http", cannot_be_a_base: false,
username: "", password: None, host: Some(Ipv4(127.0.0.1)), 
port: Some(8001), path: "/api/v1/scan", query: None, fragment: None }, 
source: TimedOut }

I don't know how to fix your timeout problem, but you need to call Response::json for reqwest to wait till the whole response body is received. Request::send returns a Response once the response headers are received and parsed, it doesn't wait for the whole body to be ready before it returns.

1 Like

The timeout problem is fixed by calling .timeout(Duration::from_secs(100)).

[...] but you need to call Response::json for reqwest to wait till the whole response body is received.

Ok, thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.