休息最佳实践:何时返回 404 not found

编程入门 行业动态 更新时间:2024-10-25 04:14:13
本文介绍了休息最佳实践:何时返回 404 not found的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有以下休息电话:

If I have the following rest call:

GET /items/{id}/subitems

在这些情况下,我们是否应该返回以下内容?:

Should we return the following in these scenarios?:

  • 如果没有找到{id},我们是否应该返回404 Not Found?
  • 如果找到 {id} 但没有找到子项,我们是否应该返回 200 Ok 和一个空数组?
  • If {id} not found, should we return 404 Not Found?
  • If {id} found but not subitems found, should we return 200 Ok and an empty array?
  • 推荐答案

    对我来说,这两个问题的答案都是是.

    For me the answer is yes for both questions.

    REST 是关于资源的.如果未找到具有给定 ID 的资源,则 404 Not Found 是最合适的响应状态.

    REST is about resources. If resource with given ID hasn't been found, then 404 Not Found is the most appropriate response status.

    但同时,GET/items/{id}/subitems/{subitem-id} 肯定会返回 HTTP 404 如果具有给定 id 的子项没有不存在.

    But at the same time, GET /items/{id}/subitems/{subitem-id} should definitely return the HTTP 404 if subitem with given id doesn't exist.

    更多推荐

    休息最佳实践:何时返回 404 not found

    本文发布于:2023-11-16 15:04:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1605363.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!