gpt4 book ai didi

http - 休息 API : 404, 422 还是 500?

转载 作者:行者123 更新时间:2023-12-03 08:56:11 24 4
gpt4 key购买 nike

假设我们有两个对象。 帖子评论帖子有n-评论

如果我想获取(或更改)帖子评论,那么我也可以做
[DELETE、GET、PATCH] posts/{postId}/comments/{commentId}
而不是
[DELETE、GET、PATCH] 获取评论/{commentId}

但是...当服务器注意到给定的 commentId 已存在,但如客户端请求所述有另一个 postId 时,会发生什么情况?那是……

  1. 404(未找到)- 因为完整路径不存在。
  2. 422(无法处理的实体)- 因为语法正确,但语义不正确。
  3. 500(内部服务器错误)- 因为这是意外情况,请参阅 ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500 )。

最佳答案

这绝对是客户端错误,因此正确的状态代码应该位于 4xx 中范围。


当无法找到给定 URI 的资源表示形式时,服务器应返回带有 404 的响应。状态码:

6.5.4. 404 Not Found

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. [...]


422状态代码旨在指示有效负载语法有效语义无效

例如,考虑一下,您有一个端点来创建评论,并且有效负载应包含此评论所属帖子的标识符:

POST /comments HTTP/1.1
Host: example.org
Content-Type: application/json

{
"content": "Awesome post!",
"postId": 1
}

在这种情况下,如果 postId 引用不存在的帖子,服务器应返回 422 .

关于http - 休息 API : 404, 422 还是 500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55166006/

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