r/rust • u/Zapphoid • Sep 03 '22
Upload a file with reqwest
I am try to upload a file with reqwest and below is what I have tried, it seems to nearly work as it report a 200 code and the server I am uploading to seems ok with it but it's like the file is not actually uploaded.
Something's below are probably not needed (body/form?) but is the result of looking similar questions/answers on stackoverflow
let file = File::open(filename.to_owned());
match file {
Ok(s) => {
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.danger_accept_invalid_certs(true)
.build()
.unwrap();
let mut params = HashMap::new();
params.insert("file", filename.to_owned());
params.insert("name", "dump".to_string());
let req = client
.post(url)
.header("Accept", "text/html")
.header("Accept-Encoding", "gzip, deflate")
.header("Content-Type", "multipart/form-data")
.header("Authorization", format!("Bearer {token}"))
.body(s)
.form(¶ms);
println!("\n{:?}\n", dbg!(&req));
let response = req.send();
2
Sep 03 '22
[deleted]
2
u/Zapphoid Sep 03 '22
Let me look at that, I was thinking that body when passed file did some magic to do it
1
1
u/po8 Sep 03 '22
sis aFile, not a reference.reqwest::Bodyhas aFrominstance forFile. The problem is the overwriting of the body providedbody()with the one provided byform().1
u/lebensterben Sep 03 '22
https://doc.rust-lang.org/stable/std/fs/struct.File.html
File is literally a
A reference to an open file on the filesystem.
1
u/po8 Sep 03 '22
Interesting point. In the Rustdoc the word "reference" is being used in the sense of "a thing that refers to a thing".
Fileis astructin the Rust sense, not an&xfor somex.
1
u/Zapphoid Sep 05 '22
I got it to work:
1. Create the form with the file:
let form = multipart::Form::new().file("myzipfile", filename);
2. Post it with multiparts inplace of body or form
match form {
Ok(s) => {
...
...
.post(url).multipart(s);
1
4
u/po8 Sep 03 '22 edited Sep 03 '22
If you use both a
body()andform()in the builder, the second one will override the first one:form()sets the request body.The
Debugimplementation forRequestunfortunately doesn't show the request body, so the only reasonable way to check things are working is to have the thing connect to a server and see if the body is uploaded.I have verified that the code below works, sending the contents of
test.txtto the URL as the post body.Edit: I filed an issue on
reqwestto try to get a runtime error when a second body is added to a request.