gpt4 book ai didi

REST HTTP 状态码(如果 DELETE 不可能)

转载 作者:行者123 更新时间:2023-12-04 22:27:11 24 4
gpt4 key购买 nike

删除 在资源上是不可能的(但与用户的权利无关)。

我们在一种资源上有一个 RESTful API。

删除 方法在资源上被授权但是在某些情况下不能删除资源(如果有数据绑定(bind)到该资源)。

在这种情况下返回给客户端的正确 HTTP 状态代码是什么?

以下是我收集的一些可能性以及为什么在我的情况下似乎不合适:

  • 403 (禁止):似乎主要与用户权限有关。
  • 405 (不允许的方法):似乎 API 并非旨在响应此类资源的此方法。
  • 409 (冲突):似乎合适,但客户端应该有可能解决与 API 的冲突,但这里不是这种情况。

  • 更新:无法通过 REST API 更改阻止资源被删除的数据绑定(bind)。但是,可以通过其他方式“释放”资源,因为其他应用程序也可以访问数据来自的数据库,这些应用程序可能会更改资源的状态(数据库中的 SQL DELETE 总是可以做到这一点)。

    最佳答案

    我会说 409 是最合适的,因为它是 RFC 中的措辞:

    The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict.



    (强调我的)

    根据我对问题描述的理解,不允许 DELETE 的原因正是与目标资源的当前状态冲突。正如 RFC 中所指出的,响应负载可以给出原因的指示,并且(可选)用户可能能够解决它。我没有在规范中看到任何使 409 不合适的内容,因为 API 不提供冲突解决的可能性。

    关于REST HTTP 状态码(如果 DELETE 不可能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122472/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com