This question already has answers here:
How to parse multipart forms using abonander/multipart with Rocket?

(2个答案)


3年前关闭。




我想使用结构在Rocket中解析HTTP POST。提交表格后失败。

我阅读了body data示例并获得了此代码。
#[derive(FromForm)]
struct ConvertFile {
    name: String,
    filename: String
}

#[post("/submit", format = "multipart/form-data", data = "<form>")]
fn submit(form: Form<ConvertFile>) {
    println!("form field: {}", form.get().name);
}

我使用curl提交:

curl -H "Content-Type: multipart/form-data" -F "name=Claus" -F "filename=claus.jpg" http://localhost:8000/submit

然后Rocket控制台回应

multipart/form-data; boundary=------------------------8495649d6ed34d20:
    => Matched: POST /submit multipart/form-data
    => Warning: Form data does not have form content type.
    => Outcome: Forward
    => Error: No matching routes for POST /submit multipart/form-data; boundary=------------------------8495649d6ed34d2.
    => Warning: Responding with 404 Not Found catcher.
    => Response succeeded.

我想提交一个文件,因此是multipart/form-data。尝试查找原因时,我在结构中使用了String使其更简单。因此,它首先以Matched:响应,然后没有匹配的路由。

这个更简单的POST可以工作:
#[post("/convert", format = "text/plain", data = "<file>")]
fn convert_file(file: String) {
    println!("file: {}", file);
}

我正在使用最新的每晚Rust与rustup。

我究竟做错了什么?

最佳答案

火箭尚不支持multipart格式。

您可以在此处查看跟踪问题:https://github.com/SergioBenitez/Rocket/issues/106

可能的解决方法在此答案中给出:How to parse multipart forms using abonander/multipart with Rocket?

09-18 23:05