HTTP获取204无内容:这是正常的 [英] HTTP Get with 204 No Content: Is that normal

查看:1042
本文介绍了HTTP获取204无内容:这是正常的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP GET请求对状态码 204 - 无内容的响应是否正常?就像,这是否就HTTP GET应该完成的语义而言是正确的?我知道 HTTP POST请求 204 - 无内容可以。对于GET请求,如果没有数据要被发回,204状态码是否合适?我应该使用404,还是坚持200为成功,但有空回应?



这个问题的用例是一个Java应用程序我正在撰写Google App Engine。我向servlet发送请求,但要发回客户端的数据将通过Channel API套接字而不是HTTP响应传输。目前,我的客户端在请求正文中发送一个没有内容的POST,并在轮询Channel API套接字之前等待来自servlet的204响应。因为在请求正文中没有发送任何数据,所以我在辩论是否让我发送一个GET而不是一个POST更合理。 解决方案 b 服务器已完成请求,但不需要返回
实体并且可能想要返回更新的元信息。
响应可以包含新的或更新的元信息,形式为
实体标题,如果存在,应该与
请求的变体相关联。




根据 RFC部分的状态码204 a>,在我看来,这是一个GET请求的有效选择。



A 404未找到 200 OK 具有空主体且 204无内容具有完全不同的含义,有时候我们不能使用正确的状态码,但是弯曲规则,他们会在一天或以后回来咬你。所以,如果你可以使用适当的状态码,使用它!



我认为GET或POST的选择是非常私人的,因为他们都会做这项工作,但我会建议您保留一个POST而不是GET,原因有两个:


  • 您需要其他部分(如果我理解正确,则为servlet)执行操作时不会从中检索某些数据。

  • 默认情况下,如果URL中没有参数,则POST请求不会被缓存。


Is it a normal occurrence for an HTTP GET Request to have a response with status code 204 - No Content? Like, is this semantically correct with respect to what an HTTP GET is supposed to accomplish? I know that a 204 - No Content is okay for an HTTP POST Request. For GET request, if no data is to be sent back, is the 204 status code appropriate? Should I use 404, or just stick to 200 for success but have an empty response?

The use case for this question is a Java application that I am writing for Google App Engine. I am sending a request to a servlet, but the data to be sent back to the client will be transmitted through a Channel API socket instead of in the HTTP Response. Currently, my client sends a POST with no content in the request body, and waits for a 204 response back from the servlet before polling the Channel API socket. Because no data I being sent in the body of the request, I am debating whether it makes more sense for me to send a GET instead of a POST.

解决方案

204 No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

According to the RFC part for the status code 204, it seems to me a valid choice for a GET request.

A 404 Not Found, 200 OK with empty body and 204 No Content have completely different meaning, sometimes we can't use proper status code but bend the rules and they will come back to bite you one day or later. So, if you can use proper status code, use it!

I think the choice of GET or POST is very personal as both of them will do the work but I would recommend you to keep a POST instead of a GET, for two reasons:

  • You want the other part (the servlet if I understand correctly) to perform an action not retrieve some data from it.
  • By default GET requests are cacheable if there are no parameters present in the URL, a POST is not.

这篇关于HTTP获取204无内容:这是正常的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆