HTTP Get with 204 No Content:这正常吗 [英] HTTP Get with 204 No Content: Is that normal

查看:33
本文介绍了HTTP Get with 204 No Content:这正常吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP GET 请求 得到状态代码204 - No Content 的响应是否正常?比如,关于 HTTP GET 应该完成什么,这在语义上是否正确?我知道对于 HTTP POST-Request204 - No Content 没问题.对于GET请求,如果没有数据回传,204状态码是否合适?我应该使用 404,还是坚持使用 200 以获得成功但响应为空?

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 concerning 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?

这个问题的用例是我为 Google App Engine 编写的 Java 应用程序.我正在向 servlet 发送请求,但要发送回客户端的数据将通过 Channel API 套接字而不是在 HTTP 响应中传输.目前,我的客户端发送请求正文中没有内容的 POST,并在轮询 Channel API 套接字之前等待来自 servlet 的 204 响应.因为我的请求正文中没有发送任何数据,所以我在争论发送 GET 而不是 POST 对我来说是否更有意义.

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 无内容

服务器已完成请求但不需要返回实体主体,并且可能想要返回更新的元信息.这响应可以包括新的或更新的元信息实体头,如果存在应该与请求的变体.

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.

根据状态代码 204 的 RFC 部分,在我看来这是 GET 请求的有效选择.

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 和 204 No Content 有完全不同的含义,有时我们无法正确使用状态代码,但违反规则,他们有一天或晚些时候会回来咬你.因此,如果您可以使用正确的状态代码,请使用它!

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!

我认为 GET 或 POST 的选择非常个人化,因为它们都可以完成这项工作,但我建议您保留 POST 而不是 GET,原因有两个:

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:

  • 您希望另一部分(如果我理解正确,则是 servlet)执行操作而不是从中检索某些数据.
  • 默认情况下,如果 URL 中不存在参数,则 GET 请求是可缓存的,而 POST 则不是.

这篇关于HTTP Get with 204 No Content:这正常吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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