问题描述
假设我们有以下休息:
GET api/companies/5
(获得ID 5的公司)
如果公司" 5"不存在,我们通常会返回404 Not Found响应.
但是现在,让我们打这个电话:
GET api/companies/5/invoices/10
(从公司5获取发票10)
现在,如果公司" 5"不存在,我们是否仍然返回404 Not Found?或仅在找不到最大资源的情况下才能返回404(在这种情况下,发票10).
Bad Request也许是一个更好的选择?
推荐答案
400不良请求意味着:
由于畸形的语法,服务器无法理解该请求.
,而404个国家:
服务器尚未找到与Request-uri匹配的任何内容.
整个URI是您的资源标识符,您找不到该特定标识符的匹配资源.
其他推荐答案
404可能会引起混乱 - 资源是否丢失或实际URL不正确?
我亲自选择422代码:
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.
问题描述
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?
推荐答案
404 is your best response. According to 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, 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.
其他推荐答案
404 may cause a confusion - is the resource missing or is the actual URL incorrect?
I'd personally go for the 422 code:
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.