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.
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 and204 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.
The POST/GET with 204 seems fine in the first sight and will also work.
Documentation says, 2xx -- This class of status codes indicates the action requested by the client was received, understood, accepted, and processed successfully. whereas 4xx -- The 4xx class of status code is intended for situations in which the client seems to have erred.
Since, the request was successfully received, understood and processed on server. The result was that the resource was not found. So, in this case this was not an error on the client side or the client has not erred.
Hence this should be a series 2xx code and not 4xx. Sending 204 (No Content) in this case will be better than a 404 or 410 response.
Your current combination of a POST with an HTTP 204 response is fine.
Using a POST as a universal replacement for a GET is not supported by the RFC, as each has its own specific purpose and semantics.
The purpose of a GET is to retrieve a resource. Therefore, while allowed, an HTTP 204 wouldn't be the best choice since content IS expected in the response. An HTTP 404 Not Found or an HTTP 410 Gone would be better choices if the server was unable to provide the requested resource.
The RFC also specifically calls out an HTTP 204 as an appropriate response for PUT, POST and DELETE, but omits it for GET.
See the RFC for the semantics of GET.
There are other response codes that could also be returned, indicating no content, that would be more appropriate than an HTTP 204.
For example, for a conditional GET you could receive an HTTP 304 Not Modified response which would contain no body content.