0

The documentation for https://docs.rs/reqwest/latest/reqwest/blocking/multipart/struct.Form.html shows this example to create a multipart::Form from a file.

    let file = reqwest::blocking::multipart::Form::new().file("key", "/path/to/file")?;

    let response = reqwest::blocking::Client::new()
        .post("https:://test.com.br/send")
        .multipart(file)
        .send()
        .unwrap();

But the function ".file" is only available on the blocking version of reqwest (reqwest::blocking::multipart::Form).

I've tested the blocking version, and i was able to send a form. But i can't find a way to do this using the async version (reqwest::multipart::Form).

Is there a alternative way to make this call using the async version?

Iago Passos
  • 1
  • 1
  • 1
  • 1
    Does this answer your question? [reqwest send multipart form with very large attachment](https://stackoverflow.com/questions/66681354/reqwest-send-multipart-form-with-very-large-attachment) – Herohtar Jan 31 '22 at 18:04

2 Answers2

2

If you want to post just a file using async, please check this answer. If you want to construct a multipart form, try to use reqwest::multipart::Part to construct a body, while wrap your file into a stream.

huangjj27
  • 51
  • 1
  • 4
0

i m successfully update file with multipart with this code(you could change patch to post)

pub async fn update_form(&self, con: &Collection, id: &str, path: &str) -> String {
    let url = [&self.url_struct(con), "/", id].concat();
    let file = fs::read(path).unwrap();
    let file_part = reqwest::multipart::Part::bytes(file)
        .file_name("bg.jpg")
        .mime_str("image/jpg")
        .unwrap();
    let form = reqwest::multipart::Form::new().part("img", file_part);
    let client = reqwest::Client::new();
    match client
        .patch(url)
        .headers(construct_headers_form())
        .multipart(form)
        .send()
        .await
    {
        Ok(res) => res.text().await.unwrap_or("no message".to_string()),
        Err(_) => "{\"error\":400}".to_string(),
    }
}