休息最佳实践:何时返回 404 not found [英] Rest best practice: when to return 404 not found

查看:60
本文介绍了休息最佳实践:何时返回 404 not found的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下休息电话:

If I have the following rest call:

GET /items/{id}/subitems

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

Should we return the following in these scenarios?:

  1. 如果没有找到{id},我们是否应该返回404 Not Found?
  2. 如果找到 {id} 但没有找到子项,我们是否应该返回 200 Ok 和一个空数组?
  1. If {id} not found, should we return 404 Not Found?
  2. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆