当给定的ID是有效的ID但没有匹配的文档时,响应状态代码应在REST API中是什么?

发布于 2025-01-29 01:59:18 字数 139 浏览 2 评论 0原文

我正在为我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2025-02-05 01:59:18

当给定ID是有效的ID但没有匹配的文档时,响应状态代码应在REST API中是什么?

404

404(找不到)状态代码指示原始服务器没有找到目标资源的当前表示形式,或者不愿意透露一个存在。

如果您需要交流为什么您要返回404,则在响应的主体

服务器应发送包含错误情况的说明的表示形式,以及它是临时情况还是永久条件。


为什么不204?

主要是因为这不是204的目的:

204响应允许服务器表明该操作已成功应用于目标资源,同时暗示用户代理不需要远离其当前的“文档视图”(如果有)。

对于GE请求,可以表明当前表示为0字节的适当方法是 content-Length 字段。

也就是说,原始服务器可以自由自由决定是否缺乏具有正确ID的文档意味着没有当前表示形式,或者当前表示为空。换句话说,原始服务器上“文档”的存在是隐藏在Web立面后面的实现细节。

What should the response status code be in a REST API when the given ID is a valid ID but there's no matching document?

404

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.

If you need to communicate why you are returning a 404, that's done in the body of the response:

the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.


Why not 204?

Primarily because that's not what 204 is for:

The 204 response allows a server to indicate that the action has been successfully applied to the target resource, while implying that the user agent does not need to traverse away from its current "document view" (if any).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文