API 从数据库获取部分数据的状态响应?

API status response for partial data being fetched from db?

如果客户端像这样请求特定范围的条目,那么适当的状态响应是什么。

GET /cars?ids=1,2,3,4,5

而且数据库只返回部分集合,所以客户端只会得到。

['car1', 'car2', car4']

我在 202 和 206 之间犹豫不决。但是处理这种情况的标准方法或多或少是什么?

我认为你应该选择 206。

202 用于

Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed.

时使用206

The server has fulfilled the partial GET request for the resource.

这是你的情况。

阅读规范:https://www.rfc-editor.org/rfc/rfc7233#section-4.1

202 代码用于范围获取;浏览器要求服务器获取响应字节范围的位置。

您可以考虑(自定义)300 级代码:

10.3 Redirection 3xx

This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request.

不幸的是,像标准的 200 级代码一样,其中 none 满足您的 部分响应列表 的情况。

202 代码在概念上用于指示服务器已 接受您发送的内容,现在将离开并处理它(异步):

The request has been accepted for processing, but the processing has not been completed. There is no facility for re-sending a status code from an asynchronous operation such as this.

我倾向于远离 200 代码,因为浏览器要求的操作没有完成。 300 代码更适合告诉发出完全合理请求的客户端,服务器无法完成所有请求 - 浏览器将不得不做更多工作才能获得完整的结果集。