Rest best practice: when to return 404 not found

2019-02-27 16:46发布

If I have the following rest call:

GET /items/{id}/subitems

Should we return the following in these scenarios?:

  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?

1条回答
Fickle 薄情
2楼-- · 2019-02-27 17:12

For me the answer is yes for both questions.

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

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.

查看更多
登录 后发表回答