【问题标题】:REST response code for accessing a corrupt/invalid resource用于访问损坏/无效资源的 REST 响应代码
【发布时间】:2016-08-08 17:57:38
【问题描述】:

对于损坏或语义无效的资源,响应 HTTP GET 的最佳 HTTP 状态代码是什么?

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

  • 404 不适用(因为数据确实存在)。
  • 4xx 总体上似乎不太理想(因为问题出在服务器端,不受客户端控制)。
  • 503 似乎适用于整个服务,而不是特定资源。
  • 500 肯定合适,但在实际告诉客户可能有什么问题时非常模糊。

有什么建议吗?

【问题讨论】:

  • IMO,500 最适合。但是,如果您查看规范,您将找不到与您描述的方式明显相关的损坏/无效资源的任何内容。 5xx 是服务器端错误,没有区分实际出了什么问题...
  • 有趣的问题...
  • 请澄清违反某些业务规则
  • 我认为 500 是唯一适合这种情况的 official 响应代码。并且没有什么可以阻止您包含描述失败原因的响应正文。
  • 这是一个有用的guide,用于选择适当的 HTTP 状态代码

标签: rest http http-status-codes


【解决方案1】:

在阅读了 cmets 和链接资源之后,看起来@RemyLebeau 的方法是最好的:

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

【讨论】:

    【解决方案2】:

    根据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 (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.
    

    【讨论】:

    • 我可以看到 503 可以 用于此,如果数据可以为将来的请求更正。但我认为 510 不适合,因为没有使用自定义扩展来访问资源。我会坚持使用 500 并带有描述失败的响应正文。
    • 我从this 了解到的是,服务器需要对请求本身进行进一步扩展才能完成它。例如,服务器错误响应可能包括该损坏数据的旧工作版本的列表,并希望您发送回相同的请求,以精确确定您需要哪个旧版本,以便可以替换或仅获取损坏的数据。你觉得@RemyLebeau 怎么样?这种情况不适合 510 状态代码的上下文吗?
    猜你喜欢
    • 2018-07-05
    • 2011-09-01
    • 1970-01-01
    • 2018-09-11
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    相关资源
    最近更新 更多