gpt4 book ai didi

rest - 如果没有可用数据,http 返回码应该是什么

转载 作者:行者123 更新时间:2023-12-04 14:41:15 26 4
gpt4 key购买 nike

例如我有一个 api 方法 /api/orders.getOrders这实际上一直存在。
如果此方法没有返回以下格式的数据,我应该发送 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 类响应)是不合适的。
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 字节长。返回空资源的非空表示时这是不合适的。

关于rest - 如果没有可用数据,http 返回码应该是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38659310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com