I'm trying to do a POST request using reqwest
. I need to send attachments in my request. I am looking for the equivalent of
curl -F attachment=@file.txt
In older versions (see here) it was as simple as
let file = fs::File::open("much_beauty.png")?;
let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
.headers(construct_headers())
.body(file)
.send()?;
But for newer versions (see here) looks like the functionality has been removed. I get the error:
21 | .body(file)
| ^^^^ the trait `From<File>` is not implemented for `Body`
|
= help: the following implementations were found:
<Body as From<&'static [u8]>>
<Body as From<&'static str>>
<Body as From<Response>>
<Body as From<String>>
and 2 others
= note: required because of the requirements on the impl of `Into<Body>` for `File`
Despite the official documentation claiming
The basic one is by using the
body()
method of aRequestBuilder
. This lets you set the exact raw bytes of what the body should be. It accepts various types, includingString
,Vec<u8>
, andFile
.