204 No Content

Sometimes you want to send an HTTP request and you don't care about the response. This is often used for logging stuff on the server. Usually you request a 1x1 pixel GIF file and send a bunch of query parameters. E.g.
htp://example.org/blank.gif?user=stoyan&browser=msie&creditcard=123JUSTKIDDING567

Such 1x1 gifs are often called "web bugs" or "beacons" and sometimes used for sneaky purposes such as checking whether-you-opened-that-email-I-sent-you-that-you-didn't-reply-to-although-I-told-you-it-was-urgent-dammit-now-I'm-gonna-tell-the-boss-yes-this-time-I'm-telling-the-boss!

Now (after we relax a bit)... instead of having your server send a blank GIF, however small, can you just send.. nothing? Yep, you can. Your server can respond with a "204 No content" header. Cool, eh, especially when you want to squeeze every bit of network traffic?

So the example above can look like:
htp://example.org/nope.php?user=stoyan&browser=msie&creditcard=123JUSTKIDDING567

And nope.php could be like:

<?php
// .... log whatever you need to log, then reply:
header("HTTP/1.0 204 No Content");
?>

Here's what this looks like in Net Panel.

204

And an example in the wild (Google search beacon)

Google search beacon

Another application of 204s I was thinking could be favicons. If you don't want to deal with favicons, but you don't want to respond with a 404 Not Found either, you can just configure Apache to send 204 to favicon requests. Could be useful when your application generates subdomains for your users, since the browser will request the favicon from each subdomian (unless you have a link-ed favicon).

This entry was posted on Sunday, March 8th, 2009 and is filed under performance. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

21 Responses to “204 No Content”

  1. Philip Tellis Says:

    It’s better to send the header, and flush the output first, and then do your logging. That way the client isn’t waiting for you to do your back end work.

  2. Feed Stats Processing Caught Up — Hobby Cash: Make Cash Blogging About the Things You Love Says:

    [...] 204 No Content / phpied.com [...]

  3. An ADSPACE preview — Hobby Cash: Make Cash Blogging About the Things You Love Says:

    [...] 204 No Content / phpied.com [...]

  4. Steve Says:

    Your URL examples are missing a ‘t’ in h-t-t-p. ;-)

  5. Stoyan Says:

    heh, true, thanks Steve.

  6. Sergey Chernyshev Says:

    Actually, it’s not so good for favicons as favicons served as 200 with cache headers can be cached by browser and never requested again. They can probably fit into the same TCP packet anyway being quite small. While 204s are probably not cached as there is nothing to cache (although I couldn’t find any specifics in HTTP spec).

  7. John Newman Says:

    Can’t you just change the method of your ajax request from GET or POST to HEAD?

    Head doesn’t return any content, just 200 and the headers. It’s essentially a 204, but not a 204. =)

  8. Performance Calendar » Blog Archive » Free-falling waterfalls Says:

    [...] way taking it out of the onload flow. Then remove that redirect. For such stats tracking cases a 204 No Content response is the appropriate way to [...]

  9. Performance Calendar » Blog Archive » Reducing the payload: compression, minification, 204s Says:

    [...] wrap up this lengthy posting with an honorable mention of the 204 No Content response (blogged before). It’s the world’s smallest componet, the one that has no body and a Content-Length of [...]

  10. rusuricacaf Says:

    lol! nice…?
    HattieInABox

  11. Shin Heitman Says:

    Very first, I’d prefer to thank you for this enlightening post. Secondly, I’d prefer to question wherever I can discover a lot more info concerning your post. I arrived right here via Bing and can’t discover any other related web websites on this subject. Just how do I subscibe for your pages? I’d prefer to adhere to your updates as they arrive along! I had a query to question but I forgot what it absolutely was… anyways, I give my thanks to you!

  12. Cold Sores On Lips Says:

    It’s appropriate time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or suggestions. Maybe you can write next articles referring to this article. I want to read even more things about it!

  13. The Lottery Black Book Review Says:

    Excellent beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog web site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear concept

  14. George Says:

    I was wondering what your opinion on below would be:
    imagine a large scale website that does analytics using yetanotherblank.gif

    would it be faster to have .htaccess rewrite rule yetanotherblank.gif -> nope.php
    where script.php would contain your code from nope.php.

    In my tests image is served much faster (you could even serve it from image host that does no script processing)..

  15. Harbor 401k rules Says:

    Safe harbor 401k rules…

    [...]204 No Content / Stoyan’s phpied.com[...]…

  16. Listing no credit check apartments in Houston Says:

    Listing no credit check apartments in Houston…

    [...]204 No Content / Stoyan’s phpied.com[...]…

  17. singapore public speaking programs Says:

    singapore public speaking courses…

    [...]204 No Content / Stoyan’s phpied.com[...]…

  18. control mantenimiento Says:

    software de mantenimiento…

    [...]204 No Content / Stoyan’s phpied.com[...]…

  19. mw3|editor|stats Says:

    mw3|editor|stats…

    [...]204 No Content / Stoyan’s phpied.com[...]…

  20. blog urgente Says:

    blog urgente…

    [...]204 No Content / Stoyan’s phpied.com[...]…

  21. Ajax with images / Stoyan's phpied.com Says:

    [...] most of the time, you can have the .php return a 204 No Response which is the smallest response (no body). If the .php determines there's something wrong, it then can return an [...]

Leave a Reply