HTTP Status: 204 No Content and 205 Reset Content
The 200 range of HTTP status codes represents successful requests. I’ve already covered 201 Created and 202 Accepted and 206 Partial Content. Today, I’ll wrap up my discussion of the 200 range by talking about 204 No Content and 205 Reset Content. The 200 OK response is probably the status with which most are familiar, and I’ll discuss it later when covering the HTTP verbs.
The 204 No Content response is useful in a web service when you want to return a success message but do not want to return a message in the body or do not have a body to return. In my personal experience, I use this when DELETE requests are sent to an Atom web service. If the resource is successfully deleted, the service returns a 204 No Content status message. This tells the client that the deletion was successful, and that’s really all the client needs to know. There’s nothing to return because it was deleted.
However, even outside of the web services realm, the 204 No Content status actually means something to a user agent (browser). The HTTP specification (RFC 2616) states the following about the 204 No Content status code:
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent’s active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent’s active view.
This means that, if I have an HTML form and submit it, then, if the server returns a 204 status code, the browser should not refresh the form or take me to another page. The document view does not change, and I remain at the form. All of the data I entered remains unchanged. All browsers I tested support this, but, in practice, it is not very useful since there’s no indication to a user that anything happened on the server side.
However, consider the use of 204 No Content in an Ajax application. If the Ajax library used simply looks for a success message of 204 in response to the requests it sends, then it can present a success message to the user without changing any of the UI elements, as this response intends. This is also useful because the service doesn’t need to send back any data; it needs only tell the client that the request was successful.
On the other hand, with a 205 Reset Content response, the intent is to tell the client to clear the content from the form or to refresh the UI. That is, I could fill out a form, click the submit button, and the form I am currently working on would refresh to its default values if the client receives a 205 response. According to the HTTP spec, a 205 response means:
The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent. This response is primarily intended to allow input for actions to take place via user input, followed by a clearing of the form in which the input is given so that the user can easily initiate another input action.
The problem is that I have not found a browser yet that supports this behavior. Browsers either treat a 205 like it’s a 204, or they simply treat it as a 200. This does not mean, though, that Ajax applications cannot properly respect a 205 response. In an Ajax application, if the client receives a 205 response (instead of a 204 response), then the UI elements should change back to their default state.
In RESTful Web Services, Richardson and Ruby make this distinction between 204 and 205:
In data entry terms: 204 is good for making a series of edits to a single record; 205 is good for entering a series of records in succession.
In my simple browser test to see what browsers supported 204 and 205 on forms (without using any Ajax), here’s what I found:
| Browser | 204 No Content | 205 Reset Content |
|---|---|---|
| Firefox 2.0.0.14 | supported | treated like 204 |
| Safari 3.1.1 | supported | treated like 200 |
| Opera 9.27 | supported | treated like 200 |
| Internet Explorer 7 (7.0.5730.13) | supported | treated like 200 |
| Internet Explorer 8 beta 1 (8.0.6001.17184) | supported | treated like 200 |
| Firefox 3 beta 5 | supported | treated like 204 |
Since I know I’ll be asked this question, to send back a 204 or 205 response with PHP, see the PHP manual on the use of the header() function.
9 Comments
Unfortunately PHP http streams don't support them. Only 200 and 206 are actually considered successful, everything else causes an error.
Jared: not anymore - there is an option for the http stream wrapper to ignore errors since 5.3 (my patch! :D), but I agree, PHP should handle any 10x/20x response as successful. Wanna shoot a mail to internals@? I'll chime in. IIRC I asked about that in a follow-up to my original suggestion, but never heard back from anyone (Sara committed the change back then)
Oh, I did check with the php-src before posting the comment, but didn't see any change to the implementation.
I posted about it to internals ~3years ago, then filed a change requests ~2 years ago... http://bugs.php.net/bug.php...
I'm going through all of your status code posts, I think they're great. Very useful, and clearer (and shorter) than going to the actual specs.
Maybe I just haven't found it yet, but do you have a chart or table somewhere that shows all of the status codes you've covered? I can sort of pull them with tags, but there are also a lot of other posts in between; I'd love to be able to reference an easily bookmarkable list if possible.
I noticed a typo in this post though:
"In RESTful Web Services, Richardson and Ruby make this distinction between 204 and 204:"
That should be between 204 and 205 :)
@HB: Thanks for catching the typo. I've corrected it.
As for having a central place to keep track of all my HTTP posts, I've set aside http://benramsey.com/http-s... just for this purpose, but I also haven't finished writing about them. I guess I should get on that. :-)
Really, I do need to wrap up this series. I'll see what I can do to make that happen soon.
Thanks for reading!
Man, thank you for the memo. I have recently started using 204 status in small intranet projects INSTEAD of ajax requests. For example, to delete an entity, I insert a simple link:
[code lang="php"]
delete
[/code]
Then, xxx.php deletes entity with given ID and sends back 204 status. And at the front-end, I just delete entity representation from the DOM tree. I know, this can't be used in serious project, but for a small something it works perfectly :)
@max - If you aren't using Ajax, I assume you can't observe the response from the server when I user clicks one of those delete links, correct?
So, if the server sends back a 500 instead, the whole page gets reloaded to show the error message, right?
Hi Ben,
Can we use 204 for Restful read requests ? For example if client sends a request looking for resource .
Server searches for the resource in DB or files and doesn't find it, does it make sense to return 204 instead of 404.
Here the debate is 404 means client side error, but in this case its not the client fault server doesn't have any resource for the request.
I don't think this makes sense. Any status from the 2xx series means that the request was successful. In this case, the request was not successful, since the client is requesting a resource that could not be found. It's still a client error, in that sense, so 404 is the appropriate response to send.