Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Get with 204 No Content: Is that normal

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?

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.

like image 624
ecbrodie Avatar asked Oct 09 '12 20:10

ecbrodie


People also ask

What does 204 no content mean?

5 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.

Should get return 204?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.

Can 204 response have body?

A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body.


2 Answers

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.
like image 135
Satevis Avatar answered Sep 27 '22 22:09

Satevis


I use GET/204 with a RESTful collection that is a positional array of known fixed length but with holes.

GET /items     200: ["a", "b", null]  GET /items/0     200: "a"  GET /items/1     200: "b"  GET /items/2     204:  GET /items/3     404: Not Found 
like image 33
Steve Mitchell Avatar answered Sep 27 '22 22:09

Steve Mitchell