404 Not Found or Bad Request?

岁酱吖の 提交于 2019-12-04 10:20:09

问题


Let's say that we have the following REST call:

GET api/companies/5 

(get company with id 5)

If company '5' doesn't exist, we would typically return a 404 Not Found response.

But now, let's take this call:

GET api/companies/5/invoices/10 

(get invoice 10 from company 5)

Now, if company '5' doesn't exist, do we still return a 404 Not Found? Or should a 404 only be returned if the outer most resource can not be found (invoice 10, in this case).

Would Bad Request perhaps be a better option?


回答1:


404 is your best response. According the HTTP RFC, http://www.ietf.org/rfc/rfc2616.txt, a 400 Bad Request means:

The request could not be understood by the server due to malformed syntax.

Whereas, a 404 states:

The server has not found anything matching the Request-URI.

The entire URI is your resource identifier, and you're not finding a matching resource for that particular identifier.



来源:https://stackoverflow.com/questions/26968479/404-not-found-or-bad-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!