当服务器上没有实现路由时,它应该return 404还是501?
When route was not implemented on server, should it return 404 or 501?
当某些路由尚未实现且资源不存在时,我总是 returned 404。例如,如果我有一个 API return 人在 /api/people
,如果我尝试 get
/api/animals
上的一个对象,我将return404.
我的同事说 404 仅用于未找到的资源,例如如果我尝试 get
/api/people/100
但没有 ID 为 100 的人。对于 /api/animals
他会return501.
对我来说,501 的意思是当我有一条不支持其中一种方法的路由时,比如我可以 get
/api/people/3
但不能 delete
它。
我决定看看 specification:
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A 404 status code does not indicate whether this lack of representation is temporary or permanent; the 410 (Gone) status code is preferred over 404 if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.
The 501 (Not Implemented) status code indicates that the server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.
对我来说,规范似乎有点模棱两可。同时有争议的是 /api/animals
没有被源站发现(404),也确实是源站没有实现请求(501)的方法。
在这种情况下,return最合适的状态码是什么?
如果你去一家书店,让柜台后面的人从书架上拿 "The best 100 cakes",而这本书没有货,他们会 return 并礼貌地说 "I'm sorry I can't find that book".在图书查找系统中将其自动化,它将 return 404.
书店的经理不会出现,而是找你书的人告诉你"I'm afraid we don't support looking for that particular book as our staff can only look for books we have" (501)。
如果你走进一家书店,让柜台后面的人给你拿割草机,他们会礼貌地回答 "I'm sorry I don't know how to do that"。那就是 501。如果你让书店里的人把你刚刚在书架上看到的书扔掉,那也是 501。
404 是 "ok, I know how to get that but it isn't there"。 501 是 "WTF! What do you think this place is?".
404 是否永久取决于回答问题的内容。如果它是 REST API,它可能会 return JSON 状态说这本书缺货但正在订购,可能有到期日。
当试图让书商扔掉书架上的一本书时,它是 501 的真正原因是,如果他们只是说 404,你只会说,"there it is there!"。他们需要说明他们不能这样做的真正原因。他们没有允许他们随意丢弃书籍而不购买的商业政策。
所以,从技术上讲,设备就在那里,他们可以走到书前,拿起它,但由于政策尚未实施,他们会以 501 停下来。
"ok, I know how to turn a REST url into functionality, that's how I know what functionality you want, but hey, we only deal with people here, 501".
当某些路由尚未实现且资源不存在时,我总是 returned 404。例如,如果我有一个 API return 人在 /api/people
,如果我尝试 get
/api/animals
上的一个对象,我将return404.
我的同事说 404 仅用于未找到的资源,例如如果我尝试 get
/api/people/100
但没有 ID 为 100 的人。对于 /api/animals
他会return501.
对我来说,501 的意思是当我有一条不支持其中一种方法的路由时,比如我可以 get
/api/people/3
但不能 delete
它。
我决定看看 specification:
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A 404 status code does not indicate whether this lack of representation is temporary or permanent; the 410 (Gone) status code is preferred over 404 if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.
The 501 (Not Implemented) status code indicates that the server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.
对我来说,规范似乎有点模棱两可。同时有争议的是 /api/animals
没有被源站发现(404),也确实是源站没有实现请求(501)的方法。
在这种情况下,return最合适的状态码是什么?
如果你去一家书店,让柜台后面的人从书架上拿 "The best 100 cakes",而这本书没有货,他们会 return 并礼貌地说 "I'm sorry I can't find that book".在图书查找系统中将其自动化,它将 return 404.
书店的经理不会出现,而是找你书的人告诉你"I'm afraid we don't support looking for that particular book as our staff can only look for books we have" (501)。
如果你走进一家书店,让柜台后面的人给你拿割草机,他们会礼貌地回答 "I'm sorry I don't know how to do that"。那就是 501。如果你让书店里的人把你刚刚在书架上看到的书扔掉,那也是 501。
404 是 "ok, I know how to get that but it isn't there"。 501 是 "WTF! What do you think this place is?".
404 是否永久取决于回答问题的内容。如果它是 REST API,它可能会 return JSON 状态说这本书缺货但正在订购,可能有到期日。
当试图让书商扔掉书架上的一本书时,它是 501 的真正原因是,如果他们只是说 404,你只会说,"there it is there!"。他们需要说明他们不能这样做的真正原因。他们没有允许他们随意丢弃书籍而不购买的商业政策。
所以,从技术上讲,设备就在那里,他们可以走到书前,拿起它,但由于政策尚未实施,他们会以 501 停下来。
"ok, I know how to turn a REST url into functionality, that's how I know what functionality you want, but hey, we only deal with people here, 501".