HTTP 204 - the forgotten status

With everyone being so hot to use Ajax for things now, it seems that people forget how much simpler things can be done if you just stop and use some old-school techniques. I know I like Ajax for some things, but if it isn't necessary, then why introduce the extra bit of complexity?

I'm speaking here about the 204 status code.

Quoting the W3

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.

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.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.


For doing simple updates, a 204 works beautifully. Pass some parameters, save some values, but leave the user right where they are. This is great for online forms where you want to save values as they get typed / changed but don't need to return anything back to the user.

One quick warning, though. PHP is notorious for not presenting proper 204's, so if you code in PHP, you'll want to hand code the entire response.

Now that you understand what it is, why would it get used?

Let's say for a minute, that you have a form that has 30 - 40 checkboxes (like for a permissions based admin section of a site) and want to turn them on and off. You don't really need to give any feedback that the permission has been added / removed, you just need to save the value.

Well, the easy answer (if you ask me) is to use a bit of javascript to request a location (document.location=_____) with some parameters for this.value and the username. When it gets requested, if it works a 204 can be returned, if not a page with an error message can be shown instead.

Or let's say you've got a whole list of options set up via radio buttons and want to enable people to answer a survey and come back later to finish. As each option gets changed, a quick request with a 204 return code could save the answer, then the response is saved for when they come back.

I haven't seen too many places utilize this type of a system, but we use it quite a bit. The real benefits are less overload on the client side, but also once the request is made, you don't have to wait for a response before the next request is made. Ajax can be slow processing a bunch of requests at times (they normally run sequentially, not in parallel), but by not using Ajax and calling a script that returns a 204, they can all be running at once if needed.

There are other status codes that don't get used too often. The full list can be found here.

Comments

Anonymous said…
Thanks Brian. This is good stuff. I arrived at your blog b/c I was trying to figure out why Google is using 204s -- it appears to me Google is using this technique to track which links are clicked from search results.
Anonymous said…
Same here got http://clients1.google.com/generate_204 in my log and I wanted to know what it was.

Popular posts from this blog

Yii multiple select dropdownlist with default values

May 2021 updates

Been getting busy