如果为了 return 数据而缺少服务,return 的 http 状态是什么

What http status to return if a service is missing in order to return data

我有一个 REST 服务 'A' 依赖外部服务 'B' 来执行任务。

如果 'B' 在 'A' 需要它来完成请求时不可用,我应该 return 什么状态?

在这种情况下 503 合适吗?在某种程度上,我们可以说服务 'A' 不可用,因为它无法执行所有工作,但实际上只有依赖服务不可用。

从API消费者的角度来看,您的服务器您正在代理的上游服务器并不重要] 不可用。您可以 return 500 or 503:

6.6.1. 500 Internal Server Error

The 500 (Internal Server Error) status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

6.6.4. 503 Service Unavailable

The 503 (Service Unavailable) status code indicates that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay. The server MAY send a Retry-After header field to suggest an appropriate amount of time for the client to wait before retrying the request. [...]

如果操作是只读的,例如,您可能想要return一些cached/default数据以避免错误。