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

查看:38
本文介绍了如果没有可用数据,http 返回码应该是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

{ "订单":[]}

推荐答案

200 是正确的.

来自 RFC 7231

状态代码的 4xx(客户端错误)类表示客户端似乎出错了.

The 4xx (Client Error) class of status code indicates that the client seems to have erred.

404(未找到)状态码表示源服务器没有找到目标资源的当前表示

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource

在您的情况下,客户在请求资源时没有犯了错误;源服务器确实找到了资源的当前表示,因此 404(实际上,整个 4xx 类响应)是不合适的.

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 也是错误的.

204(无内容)状态代码表示服务器已成功完成请求,并且响应负载正文中没有要发送的其他内容.

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.

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

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

这篇关于如果没有可用数据,http 返回码应该是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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