返回正文和 Http 204 状态是否有效?

发布于 2025-01-03 09:49:38 字数 546 浏览 0 评论 0原文

我们目前正在致力于 OData 标准:

http://www.odata.org/developers/protocols /operations

根据此标准,如果您对数据执行 PUT 操作,则应返回 204 状态代码。标准规定:

处理 PUT 请求时服务器返回状态 204(无内容) 无需响应正文即可表示成功。

现在,根据人们在其他地方读到的内容,有些地方声称,如果您返回 204,则绝对不应该返回响应正文 - 这不是是否需要的问题。

问题是我们需要归还尸体。在本例中,我们正在对一批项目执行更新查询,并且可能会部分成功(即有些已更新,有些未更新)。我们想告知客户失败的原因,而响应主体是我能想到的唯一方法。

那么这样做的“正确”方法是什么?我应该藐视 W3C 并归还尸体吗?或者我应该忽视 OData 并返回不同的响应代码?或者还有另一种可能吗?

We're currently working to the OData standard:

http://www.odata.org/developers/protocols/operations

According to this standard, if you perform an PUT operation against your data, you should return a 204 status code. The standard says that:

When processing a PUT request servers return status 204 (No Content)
to indicate success, no response body is needed.

Now, according to what one reads elsewhere, some places claim that if you're returning a 204 you should absolutely NOT return a response body - it's not a question of whether one is needed or not.

The problem is we kind of need to return a body. In this case we're performing an update query on a batch of items and partial success (i.e. some are updated, some are not) is possible. We'd like to inform the client of what the failures were and the response body is the only way of doing it that I can think of.

So what's the "correct" way of doing this. Should I flout the W3C and return a body? Or should I flout OData and return a different response code? Or is there another possibility?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

佼人 2025-01-10 09:49:38

对 PUT 的 204 响应不是 OData 服务器的强制要求(请注意,odata.org 不是规范性参考,MS-OData 文档才是)。您可以返回 200 和正文,在这种情况下,您返回的正文应该是更新实体的序列化(在服务器应用更新之后)。事实上,在 OData V3 中,我们现在支持 Prefer 标头,它正是这样做的。客户端可以在 PUT 请求中包含 Prefer 标头,并要求服务器以 200 和更新的实体进行响应。

The 204 response to PUT is not a mandatory requirement for OData servers (note that the odata.org is not a normative reference, the MS-OData document is). You can return 200 with body, in which case the body you return should be a serialization of the updated entity (after the updates were applied by the server). In fact in OData V3 we now have a support for Prefer header which does exactly this. The client can include a Prefer header with the PUT request and ask the server to respond with 200 and the updated entityt.

定格我的天空 2025-01-10 09:49:38

我认为您不得返回内容,而应考虑返回标头以指示部分完整的命令。

规范规定

204 响应不得包含消息正文,因此始终以标头字段后的第一个空行终止。

您可能还希望采用批处理方法,http://www.odata.org/developers/protocols /批次

I think you must not return content and should consider returning a header to indicate the partially complete command.

The spec states

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

You may also wish to take the batch approach, http://www.odata.org/developers/protocols/batch

硪扪都還晓 2025-01-10 09:49:38

除非操作完全成功,否则不应返回 204。

You shouldn't return 204 unless the operation succeeded completely.

浅忆流年 2025-01-10 09:49:38

对于 PUT 返回 204 似乎很奇怪。我一直认为 204 与 DELETE 配对。其中文本“无内容”表示该资源不再位于服务器上。 PUT 与删除资源没有任何关系,因此不应使用 204。另外 204 是成功代码,因此应在 100% 成功时使用。

Seems weird to return a 204 for a PUT. I always thought 204 paired with the DELETE. Where the text 'no content' means that the resource is no longer on the server. The PUT doesn't have anything to do with removing resources so shouldn't use 204. Also 204 is a success code so should be used when 100% successful.

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