正确的状态代码是 502 Bad Gateway(坏网关)。

huangapple go评论55阅读模式
英文:

What is the correct status code, if my server can't reach a third party server?

问题

我有一个服务器,它向AWS S3发出请求。

如果我的服务器对S3的请求失败了,正确的状态码是什么?


让我更详细地解释一下。

  1. “客户端”向“我的服务器”发出请求。
  2. “我的服务器”向“第三方服务器”发出请求,但失败了。
  3. 现在,“我的服务器”需要通知“客户端”,客户端的请求失败是因为对第三方服务器的请求失败。

我查找了一下,发现502 Bad Gateway可能在这种情况下是正确的,但我不确定,因为它还提到我的服务器是代理,但实际上不是。我的服务器在调用AWS S3之前以及在从AWS S3获取结果后对请求内容进行额外处理和数据库调用。

英文:

I have a server, which does a request to AWS S3.

What is the correct status code, if my server's request to S3 failed?


Let me break it down more.

  1. "Client" does request to "my server".
  2. "My server" does a request to a "third party server", which fails.
  3. Now "my server" needs to communicate to the "client", that the client's request failed because the request to the third party server failed.

I googled and found that 502 Bad Gateway, might be correct here, but I'm not sure because it also talks about my server being a proxy, which it isn't. My server does additional processing and database calls with the content of the request before the call to AWS S3, as well as after it got the result from AWS.

答案1

得分: 3

这是我的经验:

让我们考虑您的情景:

  • 我们有ServiceAServiceB
  • 客户端调用ServiceA
  • 然后ServiceA调用ServiceB
  • ServiceA将调用ServiceB的结果返回给客户端

考虑到您的ServiceA实际上执行一些业务逻辑,而不仅仅是调用ServiceB(就像代理一样),我认为更好的做法是向客户端响应200 OK,并将调用ServiceB的实际结果包装到响应数据中。

例如,像这样:

{
  "serviceBresponse": {
    "status": 503,
    ... 其他信息
  }
}

从语义上来说,这意味着以下内容:

您的ServiceA是否可响应?是的,它可以。您的ServiceA是否能够处理客户端的请求?是的,它可以。ServiceB响应了什么?请查看响应数据。

由于采用这种方法,您可以从客户端的角度区分实际上以非200状态响应的是哪个服务。如果ServiceA以非200状态响应,那么ServiceA无法处理该请求。如果ServiceA以200状态响应,那么您可以确保ServiceA正常运行。

英文:

This is from my experience:

Let's consider your scenario:

  • we have ServiceA and ServiceB
  • the client calls ServiceA
  • then ServiceA calls ServiceB
  • ServiceA returns the result of calling ServiceB to the client

Considering your ServiceA actually does some business logic instead of simply calling ServiceB (like a proxy), I think it is better to response with 200 OK to the client and wrap the actual results of calling ServiceB to response payload.

For example, like this:

{
  "serviceBresponse": {
    "status": 503,
    ... another information 
  }
}

Semantically it means the following:

Is your ServiceA respondable? Yes, it is. Is your ServiceA was able to process the client request? Yes, it was. What ServiceB responded? See the payload.

Since with this approach you can difer from client perspective which service is actually responded with non-200 status. If ServiceA responded with non-200 status, then the ServiceA was not able to process the request. If ServiceA responded with 200, then you can be sure that ServiceA is okay.

huangapple
  • 本文由 发表于 2023年2月18日 20:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定