如果没有数据,http的返回代码应该是什么?[英] What http return code should be if no data available

本文是小编为大家收集整理的关于如果没有数据,http的返回代码应该是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

例如,我具有一个API方法/api/orders.getOrders,其实际上总是存在. 如果此方法以以下格式返回数据,如果我发送404或200 HTTP响应代码?

{ "orders":[] }

推荐答案

200 是正确的.

rfc 7231

4xx(客户端错误)状态代码类指示客户似乎已erred.

404(未找到)状态代码指示原点服务器未找到目标资源的当前表示

在您的情况下,客户端DID NOT 在询问资源时犯错误;原点服务器确实找到了资源的当前表示,因此404(实际上,整个4xx响应)不合适.

204也是错误的

204(无内容)状态代码指示服务器已成功满足请求,并且在响应有效载荷体中没有发送其他内容.

"无内容"表示HTTP响应消息主体是空的,这就是返回的表示是0字节长.返回一个空资源的非空表示时不合适.

本文地址:https://www.itbaoku.cn/post/1937840.html

问题描述

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.

204 is also wrong.

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.