You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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].osu
  • audio.mp3

内容的提问来源于stack exchange,提问作者Bestfast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 03:25:17