在响应正文中返回 http 200 OK 错误 [英] Returning http 200 OK with error within response body

查看:111
本文介绍了在响应正文中返回 http 200 OK 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在服务器端发生错误时返回 HTTP 200 OK 是否正确(错误详细信息将包含在响应正文中).

I'm wondering if it is correct to return HTTP 200 OK when an error occurred on the server side (the error details would be contained inside the response body).

示例:

  1. 我们正在发送 HTTP GET
  2. 服务器端发生了意外.
  3. 服务器返回 HTTP 200 OK 状态码,在响应中有错误(例如 {"status":"some error occurred"})
  1. We're sending HTTP GET
  2. Something unexpected happened on the server side.
  3. Server returns HTTP 200 OK status code with error inside a response (e.g. {"status":"some error occurred"})

这是正确的行为吗?我们应该将状态代码更改为 200 以外的其他内容吗?

Is this the correct behavior or not? Should we change the status code to something else than 200?

推荐答案

不,这是非常不正确的.

No, this is very incorrect.

HTTP 是一种应用程序协议.200 表示响应包含表示所请求资源状态的有效负载.错误消息通常不是该资源的表示.

HTTP is an application protocol. 200 implies that the response contains a payload that represents the status of the requested resource. An error message usually is not a representation of that resource.

如果在处理 GET 时出现问题,正确的状态代码是 4xx(你搞砸了")或 5xx(我搞砸了").

If something goes wrong while processing GET, the right status code is 4xx ("you messed up") or 5xx ("I messed up").

这篇关于在响应正文中返回 http 200 OK 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆