【问题标题】:422 Error returned correctly in Postman but not in Angular 8 HTTPClient Post Request在 Postman 中正确返回 422 错误,但在 Angular 8 HTTPClient 发布请求中未正确返回
【发布时间】:2019-12-04 14:26:38
【问题描述】:

我正在 Angular 8 中测试一个简单的登录 API 调用。当我使用正确的凭据时,帖子可以正常工作并且我已登录。

当我使用无效凭据测试调用时,我在 Angular 中没有看到正确的错误响应。

当我使用我得到的无效凭据在 Postman 中测试调用时:

{ "error": { "title": "Error", "status": 422, "message": "Those details are incorrect", "code": "VALIDATION_ERROR" } }

当我使用无效的凭据在 Angular 中进行调用时,我得到以下信息:

Error Response:  {
    "headers": {
        "normalizedNames": {},
        "lazyUpdate": null,
        "headers": {}
    },
    "status": 422,
    "statusText": "OK",
    "url": "https://myServerName/login",
    "ok": false,
    "name": "HttpErrorResponse",
    "message": "Http failure response for https://myServerName/login: 422 OK",
    "error": null
}

任何想法为什么会发生这种情况?

【问题讨论】:

    标签: angular http postman http-status-code-422


    【解决方案1】:

    这是因为当您从 API 获得错误响应时,响应标头中的内容类型不是“application/json”。太有棱有角了,无法正确解析错误响应。

    【讨论】:

    • 响应头中的内容类型设置为“application/json”。这就是我在 Postman 中看到的标题:Cache-Control →no-store, no-cache, must-revalidate Cache-Control →post-check=0, pre-check=0 Connection →keep-alive Content-Length → 158 内容类型→应用程序/json; charset=UTF-8 日期 →2019 年 7 月 26 日星期五 08:59:29 GMT 到期 →1990​​ 年 7 月 26 日星期一 05:00:00 GMT Last-Modified →2019 年 7 月 26 日星期五 08:59:29 GMT Pragma →no-缓存服务器→nginx
    • 请在您的浏览器(谷歌浏览器)网络标签中匹配相同的标题。
    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 2019-02-04
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多