404 未找到或请求错误? [英] 404 Not Found or Bad Request?
问题描述
假设我们有以下 REST 调用:
Let's say that we have the following REST call:
GET api/companies/5
(获取 ID 为 5 的公司)
如果公司5"不存在,我们通常会返回 404 Not Found
响应.
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
(从公司 5 获得发票 10)
现在,如果公司 '5' 不存在,我们是否仍然返回 404 Not Found
?或者,如果找不到最外面的资源(在这种情况下为发票 10),则应该仅返回 404.
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).
Bad Request
可能是更好的选择吗?
Would Bad Request
perhaps be a better option?
推荐答案
404 是您最好的回应.根据 HTTP RFC,http://www.ietf.org/rfc/rfc2616.txt,
404 is your best response. According to the HTTP RFC, http://www.ietf.org/rfc/rfc2616.txt,
400 错误请求意味着:
A 400 Bad Request means:
由于语法错误,服务器无法理解请求.
The request could not be understood by the server due to malformed syntax.
然而,404 声明:
服务器未找到任何与请求 URI 匹配的内容.
The server has not found anything matching the Request-URI.
整个 URI 是您的资源标识符,您没有找到与该特定标识符匹配的资源.
The entire URI is your resource identifier, and you're not finding a matching resource for that particular identifier.
这篇关于404 未找到或请求错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!