0

I'm trying to send a POST request to my server including a file. I can do that with curl following this example with no problems https://gokapi.readthedocs.io/en/latest/advanced.html#interacting-with-the-api, but I can't with Rust.

When I try to implement the request with Rust I have issues, namely the file is corrupted as if it was sent in the wrong way. I tried to get it working with this code,

fn upload(file: &String) -> Result<(), Box<dyn std::error::Error>> {
    let client = reqwest::blocking::Client::new();
    let mut form = multipart::Form::new()
        .file("file", file)
        .unwrap()
        .text("allowedDownloads", "0")
        .text("expiryDays", "2")
        .text("password", "");
    let res = client
        .post("http://myserver.com/api/files/add")
        .header(ACCEPT, "application/json")
        .header("apikey", "secret")
        .header("Accept-Encoding", "gzip, deflate, br")
        .multipart(form)
        .send();
    let response_json = json::parse(&res.unwrap().text().unwrap()).unwrap();
    let id = &response_json["FileInfo"]["Id"];
    print!("http://myserver.com/downloadFile?id={}", id);
    Ok(())
}

but the server receives a bad file, 7zip gives me this error.

tried doing the same script in python, and I got it working in 3 lines.

import requests
files = {'file': ("1398608 Fractal Dreamers - Gardens Under a Spring Sky.osz", open("1398608 Fractal Dreamers - Gardens Under a Spring Sky.osz", "rb"), "application/octet-stream")}
request = requests.post("http://myserver/api/files/add", files=files, headers={'apikey': 'api'})

the file uploaded from the python script works flawlessly, while the rust doesn't. Any help is appreciated as I'm still a beginner with Rust.

I also did try Sending attachment with reqwest but I get {"Result":"error","ErrorMessage":"multipart: NextPart: bufio: buffer full"}

EDIT: the issue looks like it's related to file (being the filename) including some strange characters? Test subject file was "1398608 Fractal Dreamers - Gardens Under a Spring Sky.osz", but changing it to "a.osz" made the issue disappear. I have no clue why and how is that

content of the zip is: "Fractal Dreamers - Gardens Under a Spring Sky ([Crz]xz1z1z) [Vernal].osu" "audio.mp3"

I get the error with the full name, but "1398608 Fractal Dreamers - Gardens Under a Spring Sky.zip" works as well. What's the issue with .osz?

Bestfast
  • 1
  • 2
  • Have you tried building a file directly with [`reqwest::blocking::multipart::Part::file(file).unwrap().mime_str("application/octet-stream")`](https://docs.rs/reqwest/latest/reqwest/blocking/multipart/struct.Part.html#method.file) – PitaJ Jan 06 '23 at 23:08
  • @PitaJ yes, sadly that didn't fix my problem. I can see 2 files in my zip, but the second gives me 2 errors: Headers Error Unconfirmed start of archive and There are some data after the end of the payload data. – Bestfast Jan 06 '23 at 23:20
  • Tip: Instead of uploading a complicated file, try something simpler, like the bytes 0x00 through 0xFF and check that they arrive exactly as anticipated. If some UTF-8 mangling or CRLF conversion happens you'll see instantly. – tadman Jan 06 '23 at 23:22
  • There are two `//` missing in your `"http:myserver.com/api/files/add"`. Is that a typo or also like this in your code? – Finomnis Jan 06 '23 at 23:49
  • @tadman the file as you suggested goes smoothly, I have updated the question accordingly to the test I've already done, specifying the issue – Bestfast Jan 06 '23 at 23:51
  • @Finomnis sorry, this is a typo only in this question. – Bestfast Jan 06 '23 at 23:54

0 Answers0