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.
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.
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.
A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With