Jason
Jason

Reputation: 14605

How to properly send Etag back to web server in HTTP If-None-Match request header?

I'm trying to use caching correctly using HTTP Last-Modified and Etag headers. From what I can see, web servers send back an Etag with one of 3 formats:

When I send a new HTTP request for the same URL, I can send a header If-None-Match: <cached etag> which will cause the server to send a 304 request if it hasn't changed.

Should I remove the quotes from an Etag and send that, in order to match properly? For instance, if a standard Etag is "a8ef0-5b8e443cf07ba-gzip", should I send If-None-Match: "a8ef0-5b8e443cf07ba-gzip" or If-None-Match: a8ef0-5b8e443cf07ba-gzip?

Similarly, with a weak Etag should I remove the W/ at the beginning and also the quotes, so that if the weak etag is W/"17ba-WxSGP4eaXoBk6hGG35IOD+5mrwQ", I send a header of If-None-Match: 17ba-WxSGP4eaXoBk6hGG35IOD+5mrwQ?

Upvotes: 0

Views: 767

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 49022

The syntax for ETag, If-None-Match, and other headers related to conditional requests is defined in RFC 7232.

As can be seen there, a valid ETag requires double quotes:

ETag = entity-tag
entity-tag = [ weak ] opaque-tag
opaque-tag = DQUOTE *etagc DQUOTE

So the first example you mentioned is invalid.

If-None-Match should just be a comma-separated series of entity-tags (or *), including the double quotes and any weak indicators. The standard includes a few examples:

If-None-Match: "xyzzy"
If-None-Match: W/"xyzzy"
If-None-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"
If-None-Match: W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"
If-None-Match: *

So: send the ETag back the same way you received it.

Upvotes: 0

Related Questions