访问损坏/无效资源的REST响应代码

4

当对一个语义无效或损坏的资源进行HTTP GET响应时,什么是最好的HTTP状态码呢?

例如,考虑对GET /person/1234的请求,其中服务器存在ID为1234的人员数据,但违反了某些业务规则,因此服务器拒绝使用它。

  • 404不适用(因为数据实际上存在)。
  • 总的来说,4xx似乎不理想(因为问题在服务器端,而不是客户端控制之下)。
  • 503似乎适用于整个服务,而不是特定的资源。
  • 500确实适用,但在实际告诉客户端可能出错的情况下非常模糊。

有什么建议吗?


1
在我看来,500 是最合适的。但是如果你查看规范,你不会找到任何明显与你所描述的损坏/无效资源相关的内容。5xx 是服务器端错误,没有区分实际出了什么问题... - Miloš Đakonović
1
我认为500是唯一适合这种情况的官方响应代码。而且,你可以包含一个响应体来描述失败的原因,没有任何阻止。 - Remy Lebeau
1
这是一个有用的指南,可以帮助您选择适当的HTTP状态码。点击此处查看。 - Alexandru Marculescu
@CássioMazzochiMolin - 在这种情况下,提供资源涉及读取一些服务器端专有格式的数据文件,由于某些我无法控制的情况,这些文件可能会损坏或无效。 - Josh Kelley
@Adrien - 客户定制。谢谢。 - Josh Kelley
显示剩余3条评论
2个回答

1
阅读评论和相关资源后,看起来@RemyLebeau的方法是最好的:

我认为500是唯一适合这种情况的官方响应代码。并且没有任何阻止你包含一个描述失败原因的响应正文。


0
根据 iana.org
4xx: Client Error - The request contains bad syntax or cannot be fulfilled
5xx: Server Error - The server failed to fulfill an apparently valid request

我认为在内部服务器错误或迁移等情况下,没有任何一个4xx状态码应该作为响应。除非包括用户预填充的数据,比如用户的套餐不允许他在预定和已知日期之后访问该数据,在这种特定情况下,可能会使用403 Forbidden,就像@Bari建议的那样。

我不是专家,但我认为当服务器拒绝或决定将端点数据视为损坏或无效时,取决于接下来应该做什么。我看到3种可能的情况:

1. 预计以某种方式会修复此问题,并且应邀请客户端在未来的某个时刻重新请求它==>503 (服务不可用)

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
   (Section 7.1.3) to suggest an appropriate amount of time for the
   client to wait before retrying the request.

2. 有些问题出现了,这不是客户端的责任,但有一种替代方法可以访问数据,可能需要遵循特定的流程或发送更多详细信息 ==> 510未扩展

2. 服务器无法满足请求,但有一种替代方法需要包含更多详细信息。 例如:当请求的数据损坏时,服务器错误响应可能包括一个旧版本(或未保存、未版本化)的列表,并期望客户端更具体地选择要获取的版本,而不是损坏的版本。 ==> 510未扩展

510 Not Extended

   The policy for accessing the resource has not been met in the
   request.  The server should send back all the information necessary
   for the client to issue an extended request. It is outside the scope
   of this specification to specify how the extensions inform the
   client.

   If the 510 response contains information about extensions that were
   not present in the initial request then the client MAY repeat the
   request if it has reason to believe it can fulfill the extension
   policy by modifying the request according to the information provided
   in the 510 response. Otherwise the client MAY present any entity
   included in the 510 response to the user, since that entity may
   include relevant diagnostic information.
  • 2 个案例已更新,包括一个示例,因为在我看来它可能适用于这种情况。但是再次声明,我不是任何专家,我可能是错误的。

3. 没有其他替代方法,没有期望的结果或者其他情况 ==> 500 应该是好的。

500 (Internal Server Error) 

   status code indicates that the server
   encountered an unexpected condition that prevented it from fulfilling
   the request.

我可以看出503 可能 用于此,如果数据可以为未来的请求进行校正。但我认为510不适合,因为没有使用自定义扩展来访问资源。我建议使用500,并提供响应体描述失败原因。 - Remy Lebeau
我从这里了解到的是,服务器需要进一步扩展请求本身才能满足它。例如,服务器错误响应可能包括一个旧版本的工作数据列表,并期望您发送回相同的请求,明确指定您需要哪个旧版本,以便替换或获取损坏的数据。你认为@RemyLebeau呢?这种情况是否符合510状态代码的上下文? - Salem Ouerdani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接