在响应正文中返回http 200 OK并出错

问题描述:

我想知道当服务器端出现错误并且响应正文内部出现错误时,返回 HTTP 200 OK 是否正确。

I'm wondering if it is correct to return HTTP 200 OK when error on server side occurred with some error inside of response body.

示例:


  1. 我们发送 http GET

  2. 服务器端出现意外情况。

  3. 服务器返回 http 200 OK 状态响应中包含错误的代码(例如 {status:发生了一些错误}

  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 occured"}

是否是正确的行为?我们不应该更改状态代码吗?

Is is correct behavior or not? Shouldn't we change status code?

不,这是非常的错误。

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").