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

时间:2016-08-08 17:57:39

标签: rest http http-status-codes

对于腐败或语义无效的资源,使用HTTP GET时最好的HTTP状态代码是什么?

例如,考虑向the-button-blue请求服务器上存在人员ID 1234的数据但违反某些业务规则的请求,因此服务器拒绝使用它。

  • 404不适用(因为数据实际存在)。
  • 4xx一般看起来并不理想(因为问题出在服务器端,而不是客户端的控制)。
  • 503似乎适用于整个服务,而非特定资源。
  • 500当然适合,但实际上告诉客户可能出现的问题非常模糊。

有什么建议吗?

2 个答案:

答案 0 :(得分: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 as @Bari确实建议。

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

1。预计不知何故,这将是修复和客户端     应邀请它回来,但在将来的某个时刻==> 503 (Service Unavailable)

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 Not Extended

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

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.

答案 1 :(得分:0)

阅读评论和链接资源后,看起来@ RemyLebeau的方法最好:

  

我认为500是唯一适合这种情况的官方响应代码。并且没有什么可以阻止你包括描述失败原因的响应机构。