Send a file using Multipart form with reqwest

Hi !

I'm Pascal, I have a PHP background and have started to learn Rust for a few months.
As first small project I would like to create a small library which can communicate with a web service.
For some purpose I need to send a file in a multipart form.
As there is a lot of libraries which can help me to do this I have chosen to give a try to reqwest.
I don't find in the documentation an example of how I can implement this and it's difficult for me to have a picture how I can process.
I have already written this library in PHP therefore I thought this could be the best way for me to start learning.

Is there is an 'officiel' library to use for this like 'rand' is?
Can I use httpmock to test my request or something else?

Thanks you in advance!


1 Like

There's no official library for http, but reqwest is definitely the most widely used library out there for this purpose.

You can find an example on this page. I have included it below:

use reqwest::blocking::multipart;

let form = multipart::Form::new()
    // Adding just a simple text field...
    .text("username", "seanmonstar")
    // And a file...
    .file("photo", "/path/to/photo.png")?;

// Customize all the details of a Part if needed...
let bio = multipart::Part::text("hallo peeps")

// Add the custom part to our form...
let form = form.part("biography", bio);

// And finally, send the form
let client = reqwest::blocking::Client::new();
let resp = client

Be aware that reqwest is split into a blocking an async version. The above example is from the blocking version of reqwest, which is found under the reqwest::blocking module. The documentation link that you have in your post links to the async version, which is why it looks different. I used the blocking version because it didn't sound like you had a use-case where async makes sense.

Thanks a lot Alice!
I will try in this direction :slight_smile:

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.