微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。