简体   繁体   English

API调用多个API时部分响应的Http状态

[英]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.

相关问题 Restful Api的最佳HTTP响应代码,当发生故障时可以调用其他Web服务 - Best HTTP Response Code for a Restful Api which makes calls to other Web Services when a failure occurs Spring类AuthenticationProvider中如何在403 http状态的情况下拦截来自REST API的响应 - How to intercept the response from REST API in case of 403 http status, in Spring class AuthenticationProvider 正确的 REST API 响应 HTTP 状态代码 - Correct REST API Response HTTP Status Code 创建一个API来处理对多个API的调用以进行集成的优良作法 - Is it good practice to create an API to handle calls to multiple APIs for integration Angular 8 - 为什么客户端对后端进行多次 API 调用 - Angular 8 - Why client makes multiple API calls to back-end 如果服务器上没有可用的资源版本,HTTP状态代码将执行什么操作 - What HTTP Status code makes in case a version of the resource asked is not available on server 由于业务异常导致的错误响应的Http状态。 语法是正确的,请求是有意义的 - Http status for error response because of business exception. Syntax is correct and request makes sense REST API针对非法操作的最佳HTTP状态响应 - REST API best HTTP Status response for illegal operation 自定义响应+ HTTP状态? - Custom Response + HTTP status? Rest API:应用语义错误的响应 HTTP 状态码 - Rest API: Response HTTP status code for applicative semantic error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM