Skip to content

Instantly share code, notes, and snippets.

@bxcodec
Forked from jaredhirsch/gist:4963424
Created December 21, 2018 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bxcodec/92201e2035a1d580b251fcafad1721bf to your computer and use it in GitHub Desktop.
Save bxcodec/92201e2035a1d580b251fcafad1721bf to your computer and use it in GitHub Desktop.
ETags & If-None-Match headers: a dialogue

ETags & If-None-Match headers: a dialogue

1st request.

browser: can haz foo?

GET /foo HTTP/1.1

1st response.

server: o hai, dis version 12345.

note, there's always an empty line between headers & body.

HTTP/1.1 200 OK
ETag: "12345"

<!doctype html><p>foo.

2nd request.

browser: hai. can haz latest? i haz 12345.

GET /foo HTTP/1.1
If-None-Match: "12345"

2nd response if current version is unchanged

server: u haz latest lol!

HTTP/1.1 304 Not Modified

2nd response if current version has changed

if page has changed + new version is 56789,

server: lol wut! herez latest

HTTP/1.1 200 OK
ETag: "56789"

<!doctype html><p>new foo.

Because the 304 has no response body, client and server save bytes & time.

GET requests with If-None-Match headers are called conditional GET requests, since the server only returns a response body if there's new content. You can also use date-based validation to issue conditional GETs, saving transfer bandwidth without implementing ETags. The flow is the same, except the server sends down an Expires date, and the browser sends it back as a Last-Modified header.

See the HTTP 1.1 RFC for more details. It's more readable than you'd think.

Also, those are valid html5 documents in the examples; I just omitted optional tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment