[英]Http Status for partial response in the case where API makes calls to multiple APIs
I have an API whose job is to aggregate resources obtained by calling multiple other APIs and then give out the aggregated response to the client.我有一个 API,它的工作是聚合通过调用多个其他 API 获得的资源,然后将聚合的响应发送给客户端。 Currently even if one or more dependent API calls fail, I go ahead and aggregate response from the other dependent APIs and then give them out to the client with a 2xx status.目前,即使一个或多个相关 API 调用失败,我也会继续聚合来自其他相关 API 的响应,然后将它们以 2xx 状态发送给客户端。 In case all of the dependent API calls fail, I still give out a 2xx with empty list.如果所有依赖的 API 调用都失败,我仍然会给出一个空列表的 2xx。
In case one or more dependent API calls fail, I want to give out an indication to the client.如果一个或多个相关 API 调用失败,我想向客户端发出指示。 I was thinking of using the HTTP code 206 .我正在考虑使用 HTTP 代码206 。 What would be the best approach to handle this scenario.处理这种情况的最佳方法是什么。 Is giving out a 206 with the names of the dependent services that failed to give out a 2xx as part of a header the correct approach.给出一个 206 与依赖服务的名称未能给出 2xx 作为标题的一部分的正确方法。 If yes, what should this header be called?如果是,这个标题应该叫什么?
I was thinking of using the HTTP code 206.我正在考虑使用 HTTP 代码 206。
I can't promise, but that seems like a very bad idea;我不能保证,但这似乎是一个非常糟糕的主意; see RFC 7233见 RFC 7233
The 206 (Partial Content) status code indicates that the server is successfully fulfilling a range request 206 (Partial Content) 状态码表示服务器成功完成了一个范围请求
If the client didn't send you a range request (see RFC 7233 for the specifics), then I don't think there is any benefit in answering as if it had.如果客户端没有向您发送范围请求(有关详细信息,请参阅 RFC 7233),那么我认为回答好像没有任何好处。 General-purpose components are likely to get confused.通用组件可能会混淆。
As for what status code to use instead: the response code, like the headers, is meta data so that general purpose components can understand what is going on and provide intelligent assistance.至于改用什么状态代码:响应代码,就像标头一样,是元数据,以便通用组件可以了解正在发生的事情并提供智能帮助。 The details of what you are doing, that are to be understood by the bespoke client, belong in the response body.您正在执行的操作的详细信息,由定制客户端理解,属于响应正文。
(Analogy - on the web, the status code and the headers are for the (general purpose) browser. The response body is for the human being.) (类比 - 在网络上,状态代码和标题是针对(通用)浏览器的。响应主体是针对人类的。)
200 OK is probably what you want to be using here; 200 OK可能是你想在这里使用的; with a "representation of the status of the action" in the payload.在有效载荷中带有“动作状态的表示”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.