Track large file upload progress with reqwest

Does anyone have advice on how (or if it's possible) to track the progress of a file upload via multipart POST request using the reqwest library?

I'm thinking of something along the lines of this from the JS world: XMLHttpRequest: progress event - Web APIs | MDN, where you can listen for a "progress" event.

My current code looks something like this and it works (I'm using tokio runtime btw)

pub async fn my_uploader() -> Result<(), Box<dyn std::error::Error>> {
    use reqwest::multipart::{Form, Part};
    use reqwest::Client;
    use tokio::fs::File;

    let url = "https://my_cool_post_url.com";

    let filename = "test_file.txt";
    let file = File::open(filename).await?;
    let length = file.metadata().await?.len();

    let upload_part = Part::stream_with_length(file, length).file_name(filename);
    let form = Form::new().part("upload", upload_part);
    let client = Client::new();
    let res = client
        .post(url)
        .bearer_auth("abcdefg")
        .multipart(form)
        .send()
        .await?;
    println!("Status: {}", res.status());
    println!("Headers:\n{:#?}", res.headers());
    println!("Body:\n{}", res.text().await?);
    Ok(())
}

I don't technically need the progress, but it would be nice to expose an interface that a caller could use to track it for large slow uploads.

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.