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

查看:366
本文介绍了如果没有可用数据,应该返回什么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?

{ "orders":[] }

{ "orders":[] }

推荐答案

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天全站免登陆