gpt4 book ai didi

rest - 对于依赖于其他内容的 DELETE,适当的 http 响应代码是什么?

转载 作者:行者123 更新时间:2023-12-03 07:09:02 26 4
gpt4 key购买 nike

假设我想要DELETE/groups/20,并且我的业务规则规定,只要群组有成员,您就无法删除该群组。

  • 如果 DELETE 成功,我将返回 200204
  • 如果组 20 不存在,我将返回 404
  • 如果用户未经过身份验证,我将返回 401
  • 如果用户已通过身份验证,但没有组 20 的权限,我将返回 403
  • 如果组 20 存在,用户已通过身份验证,请求正常,但您无法删除组 20,因为它包含成员?

在这种情况下你会返回什么? 400?但语法是完美的。 403?嗯,是的,这是被禁止的,但是 403 通常意味着“您无权执行此操作”,而不是“您有权利,但有些先决条件没有满足,处理好这些然后再回来。”

最佳答案

如果用户能够通过删除成员并尝试再次删除群组来更正请求,则您应该返回 409 ConflictRFC 7231说:

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.

您在评论中说409 Conflict似乎更多地是为双重创建某些东西而保留的,但“似乎是保留的”几乎是一个矛盾的说法。如果保留某些内容,RFC 通常对此非常明确并使用关键字 MUST。 RFC 7231 提到由于 PUT 请求而导致的冲突编辑情况是更常见的情况,但没有提到状态代码是为此保留的。

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the representation being PUT included changes to a resource that conflict with those made by an earlier (third-party) request, the origin server might use a 409 response to indicate that it can't complete the request. In this case, the response representation would likely contain information useful for merging the differences based on the revision history.

关于rest - 对于依赖于其他内容的 DELETE,适当的 http 响应代码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26698386/

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