Skip to content

Instantly share code, notes, and snippets.

@bxcodec
Forked from jaredhirsch/gist:4963424
Created December 21, 2018 14:22
Show Gist options
  • Select an option

  • Save bxcodec/92201e2035a1d580b251fcafad1721bf to your computer and use it in GitHub Desktop.

Select an option

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 page version is 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: **you haz latest!**
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>foo. too.
### 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](http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.2) 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](http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#syntax-tag-omission).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment