You can make an HTTP request with a custom body as multipart form data in Rust by utilizing a library called reqwest
.
To use this library, you can add the following line to your Cargo.toml
file:
74 chars3 lines
And then you can use the following code to make a POST request with a multipart form data body:
main.rs682 chars28 lines
In this code, you first create a Client
instance from the reqwest::blocking
module. You then read the contents of your file into a byte vector.
Next, you create a Form
instance by using the Form::new()
method, and add the file contents as one of the parts of the form. The Part::bytes()
method is used to create a Part
instance from the reqwest::multipart
module containing the byte vector, and file_name
and mime_type
are used to set the name and mime-type of the file to be uploaded.
Finally, you make a POST request to your desired URL by chaining .post()
to your client
, passing in the URL to post to, and then chaining .multipart()
to this with the Form
instance as its argument. You can then call send()
on the request object to execute it, and inspect the returned response object as desired.
gistlibby LogSnag