使用Rocket

时间:2017-07-25 21:57:24

标签: forms rust http-post rust-rocket

我想使用struct解析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);
}

我正在使用最新的每晚锈病。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

Rocket尚不支持multipart表单。

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

此答案中提供了一种可能的解决方法:How to parse multipart forms using abonander/multipart with Rocket?