What is a HTTP HEAD request good for? – Some uses

by Ochronus on November 5, 2010

Let’s meet HTTP GET ‘s little brother

According to Wikipedia:

Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

This means that we have a faster way of checking the headers and some server info for a given resource on the server. So the first trivial way of using a HTTP HEAD request is checking if a given url is serviceable, a given file exists, etc.. This can be used for example for creating a faster link verification service.

Also HEAD returns the full headers, so we can do a LastModified/ContentLength check to decide if we want to re-download a given resource.

There’s another simple use of HEAD: you can log js based content appearances. For example you have an ad service which is js based and you’d like to log the number of appearances. You could naively count the number of downloads in the web server’s access log to that specific js but that has some false positives so you’d better move the ‘I’m shown’ logic to the ad itself. The classic way is counting the number of image accesses or adding a special 1-pixel image, but there’s also a simpler way: just have the js code make a HEAD request to the server. You can then configure the server to log those HEAD requests separately and voila – you have your ad display stats with near zero resource usage (at least in regards of serving content) on your server.

These are just some basic ideas, I feel that HTTP HEAD is unfairly forgotten.

  • http://onconnex.com Ram S.

    The advantage of using 1-pixel image is that it is compatible with most browser or ones blocking JS.

    • http://www.iamnolegend.com ochronus

      That’s right but I was considering an already js based solution. Static banners are quite limited. We’re using some js based dynamic ads and we do content matching on the target page.

  • Chris

    These are not really forgotten.
    But they are used more for http related things, like caching.

    The head idea for counting is a nice idea, because it doesn’t move data, but I’m not sure I’d call it simpler.

  • Angelo

    A GET that returns a zero-length entity-body is no different than a HEAD in this case. However, both GET *and* HEAD imply idempotence (wiktionary: Describing an action which, when performed multiple times, has no further effect on its subject after the first time it is performed.) POST with a zero-length entity-body would be most semantically appropriate.

  • Chris

    Another advantage of tracking pixels for metrics is that they’ll work cross domain, so you can centralize or outsource your tracking (à la Google Analytics). If you want to issue an XHR with the HEAD method, you’ll need to install your tracking software or log parser on each domain from which you serve pages.

  • James

    One fantastic use of the HEAD command is monitoring. Have an application delivery controller or load balancer managing your servers? Send: HEAD / HTTP/1.1, check for ^HTTP/1.[01] 200 OK$ in the return, and you can’t be faked out by content containing the check string.

Previous post:

Next post: