Rocket - json 中的默认捕获器响应而不是 html

问题描述

我已经配置了一个非常简单的 Rocket 端点,它使用 JSON 负载响应 POST 请求。 每当此端点返回错误时,认捕获器都会返回 HTML 负载

curl -XPOST -H "Content-type: application/json" -H "Accept: application/json" http://localhost:8000/auth -d '{"username":"test"}'

            <!DOCTYPE html>
            <html lang="en">
            <head>
                <Meta charset="utf-8">
                <title>422 Unprocessable Entity</title>
            </head>
            <body align="center">
                <div role="main" align="center">
                    <h1>422: Unprocessable Entity</h1>
                    <p>The request was well-formed but was unable to
                be followed due to semantic errors.</p>
                    <hr />
                </div>
                <div role="contentinfo" align="center">
                    <small>Rocket</small>
                </div>
            </body>
            </html>

根据文档 here,如果请求定义了 application/json Accept 标头,它应该返回一个 JSON 有效负载,但我似乎没有发生这种情况。

使用的函数定义如下:

#[post("/auth",format = "json",data = "<creds>")]
fn auth(conn: UsersDbConn,creds: Json<Credentials>) -> Json<Response> {
    let c = format!("{}:{}",creds.username,creds.password);
    Json(Response { error: c })
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)