如果没有可用数据,http return 代码应该是什么

What http return code should be if no data available

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

{ "orders":[] }

200 是正确的。

来自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

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

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.

“无内容”表示 HTTP 响应消息主体为空,也就是说返回的表示形式为 0 字节长。返回空资源的非空表示时不合适。