60

I've read the w3.org spec on the 'HEAD' verb, and I guess I'm missing something. I can't see how it would be useful.

Is the HTTP 'HEAD' verb useful in web development?

If so, how?

Dan Esparza
  • 28,047
  • 29
  • 99
  • 127

3 Answers3

60

From RFC2616:

This method (HEAD) can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The reason why HEAD is preferred to GET is due to the absence of the message body in the response making it using in scenarios where you want to determine if the content has changed at all - a change in the last modified time or content length usually signifies this.

Also, a HEAD request will provide some information about the server setup (whether it is IIS/Apache etc.), unless the server was masked; of course, this is available in all responses, but HEAD is preferred especially when you don't know the size of the response. HEAD is also the easiest way to determine if a site is up or down; again the irrelevance of the message body makes HEAD the ideal candidate.

I'm not sure about this, but RSS/ATOM feed readers would use HEAD over GET to ascertain if the contents of the feed have changed.

Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • 4
    To add an example: I'm building a "customers" API that serves data to multiple apps over a REST API. The apps will want to regularly check to see if the customer has been updated, but we don't want to do all the DB joins or keep re-sending the whole customer over the wire. Instead, we repeatedly hit the API with a HEAD request at /customer/XYZ, letting us know when there's last been a change. – Mike Marcacci Feb 21 '14 at 23:43
  • Want to add another example, even though this is an old post. ElasticSearch recommends using HEAD commands if you want to see if a document exists. – Darren Hoehna Feb 03 '16 at 19:51
  • You don't use HEAD to see if a document has changed, you use `If-Modified-Since` or `If-None-Match` (e.g. take a look in your browser network log for "304 Not Modified" responses while reloading this page). And HEAD is (at least theoretically) fully redundant as of HTTP/1.1, which allows `Range: bytes=0-0`. – Søren Løvborg Sep 22 '17 at 16:17
30

The HTTP HEAD can also be used to pre-authenticate to web server, before you do HTTP PUT/POST of some large data. Without the first HEAD request, you would be sending the large data to web server twice (because the first request would return 401 unauthorized reponse with WWW-authenticate header).

Peter Sladek
  • 900
  • 9
  • 8
5

It's mainly for browsers and proxies to determine whether they can use a cached copy of the web document without having to download the whole thing (which would rather defeat the purpose of a cache).

chaos
  • 122,029
  • 33
  • 303
  • 309
  • 7
    Do any current browsers do this? I thought they would just use a "GET" but include an "If-Modified-Since" request header. – David Sep 22 '09 at 19:01
  • Dunno. From what I can see in Tamper Data, Firefox sure doesn't. I suppose it would be more useful when there are more criteria than modification time involved, which would be more proxies than browsers. – chaos Sep 22 '09 at 19:04
  • David is right, unless there is a browser that does this. It is disadvantageous since the response will indicate whether the client should download the content or not, resulting in 2 requests instead of one. Probably useful for other HTTP clients though. – Vineet Reynolds Sep 22 '09 at 19:04
  • 1
    @chaos, yes proxy caches might be using HEAD. – Vineet Reynolds Sep 22 '09 at 19:05