当给定的ID是有效的ID但没有匹配的文档时,响应状态代码应在REST API中是什么?
我正在为我的Express应用程序使用MongoDB,并且我知道,当ID是相应数据库的无效ID时,响应状态代码为400(不良请求)。但是,如果ID是有效的ID,但没有该ID的文档(例如,可以删除文档)怎么办?我猜想状态代码应该是404,但我不确定,您的意见是什么?
I'm using MongoDB for my Express app and I know that when an ID is an invalid ID for the corresponding database, the response status code is 400 (Bad Request). But what if the ID is a valid ID but there's no document with that ID (for example the document might be deleted)? I'm guessing the status code should be 404 but I'm not sure, what are your opinions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
404
如果您需要交流为什么您要返回404,则在响应的主体:
主要是因为这不是204的目的:
对于GE请求,可以表明当前表示为0字节的适当方法是 content-Length 字段。
也就是说,原始服务器可以自由自由决定是否缺乏具有正确ID的文档意味着没有当前表示形式,或者当前表示为空。换句话说,原始服务器上“文档”的存在是隐藏在Web立面后面的实现细节。
404
If you need to communicate why you are returning a 404, that's done in the body of the response:
Primarily because that's not what 204 is for:
For a GET request, the appropriate way to signal that the current representation is 0 bytes long is a 200 response with a Content-Length field.
That said, the origin server has the freedom to decide for itself whether the absence of a document with the right id means there is no current representation, or if the current representation is empty. In other words, the existence of "documents" at the origin server is an implementation detail that is hidden behind the web facade.