使用reqwest发送POST请求时文件损坏问题求助
Rust reqwest 上传带特殊文件名的.osz文件损坏问题
我尝试向服务器发送包含文件的POST请求,curl和Python requests都能正常实现,但用Rust reqwest库上传后文件损坏,无法正常打开(7zip报错)。
我的Rust代码如下:
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(()) }
Python requests仅用3行代码就能正常上传:
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'})
我尝试过Stack Overflow上的相关方案,但得到错误:
{"Result":"error","ErrorMessage":"multipart: NextPart: bufio: buffer full"}
编辑补充:
- 问题似乎和文件名及后缀有关:当测试文件名为
1398608 Fractal Dreamers - Gardens Under a Spring Sky.osz时,上传后文件损坏;改为a.osz后问题消失。 - 把该文件后缀改成.zip,使用完整原名上传却能正常完成,.osz后缀的问题出在哪里?
该压缩包包含以下内容:
Fractal Dreamers - Gardens Under a Spring Sky ([Crz]xz1z1z) [Vernal].osuaudio.mp3
内容的提问来源于stack exchange,提问作者Bestfast
相关产品推荐
相关产品推荐

