问题描述
{ "orders":[] }
推荐答案
200 是正确的.
从 rfc 7231
4xx(客户端错误)状态代码类指示客户似乎已erred.
在您的情况下,客户端DID NOT 在询问资源时犯错误;原点服务器确实找到了资源的当前表示,因此404(实际上,整个4xx响应)不合适.
问题描述
For example i have an api method /api/orders.getOrders which actually always exists. If this method returns no data in following format, should i send 404 or 200 http response code?
{ "orders":[] }
推荐答案
200 is correct.
From RFC 7231
The 4xx (Client Error) class of status code indicates that the client seems to have erred.
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource
In your case, the client did not make a mistake in asking for the resource; the origin server did find a current representation of the resource, so 404 (indeed, the entire 4xx class of responses) is not appropriate.
The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body.
"No content" means that the HTTP response message body is empty, which is to say the representation being returned is 0 bytes long. It's not appropriate when returning a non empty representation of an empty resource.