0

I want to execute a post request using reqwest and retry it in case of error. To retry I am using again crate. My code looks like this:

pub async fn post_part(&self, url: &str, headers: HeaderMap, body: Form) -> Result<Response, reqwest::Error> {
    let response = self.retry_policy.retry( || {
        self.client.request(Method::POST, url).headers(headers.clone()).multipart(body).send()
    }).await?;
    Ok(response)
}

Getting this error:

let response = self.retry_policy.retry( || {                                          
    -----  ^^ this closure implements `FnOnce`, not `FnMut`                                          
                                      the requirement to implement `FnMut` derives from here
         self.client.request(Method::POST, url).headers(headers.clone()).multipart(body).send()                                                                                                   ---- closure is `FnOnce` because it moves the variable `body` out of its environment

self.retry_policy:

again::RetryPolicy::fixed(Duration::from_millis(1000))
        .with_jitter(true).with_max_retries(5)
        .with_max_delay(Duration::from_millis(2000));

Need help to overcome this issue.

zeeshan
  • 33
  • 1
  • 7
  • I think you may misunderstand how move closures work. The move variant of the one you have would just be `move || { ... }` – PitaJ Sep 21 '22 at 14:59
  • It looks like `Form` doesn't implement `Clone`, so it may not be possible to do this how you're trying. – PitaJ Sep 21 '22 at 15:07
  • yes, I have edited the question. Apart from this implementation, do you see a way around to implement retry. – zeeshan Sep 21 '22 at 18:57

0 Answers0