我正在尝试制作一个表单来在 Rocket Rust 中创建帖子(我还没有访问数据库)。当我尝试提交表单时,我收到错误 422。
错误:
POST /new_post application/x-www-form-urlencoded:
=> Matched: POST /new_post (new_post_form)
=> Error: The incoming form failed to parse.
=> Outcome: Failure
=> Warning: Responding with 422 Unprocessable Entity catcher.
=> Response succeeded.
处理程序代码:
#[post("/new_post", data="<form>")]
fn new_post_form(form: Form<NewPostForm>) -> Flash<Redirect> {
let form = form.into_inner();
if form.body.is_empty() {
Flash::error(Redirect::to("/new_post"), "NoBodyError");
}
if form.name.is_empty() {
Flash::error(Redirect::to("/new_post"), "NoNameError");
}
if form.name.is_empty() && form.body.is_empty() {
Flash::error(Redirect::to("/new_post"), "NoNameBodyError")
} else {
Flash::error(Redirect::to("/new_post"), "NoNameBodyError")
}
}
表单结构:
#[derive(FromForm)]
pub struct NewPostForm {
pub name: String,
pub body: String
}
HTML 表单:
<form action="/new_post" method="post">
name:<input type="text" name="name" id="name">
Body:<input type="text" name="body" id="body">
<button type="submit" name="button"></button>
</form>
PS 我认为问题出在 HTML 表单中,而不是 Rust。如果问题出在 Rust 中,它会给出另一个错误。