Chrome is not sending if-none-match

avances123 picture avances123 · Mar 29, 2014 · Viewed 10.6k times · Source

I'm trying to do requests to my REST API, I have no problems with Firefox, but in Chrome I can't get the browser to work, always throws 200 OK, because no if-none-match (or similar) header is sent to the server.

Chrome request

With Firefox I get 304 perfectly.

Firefox request

I think I miss something, I tried with Cache-Control: max-age=10 to test but nothing.

Answer

natevw picture natevw · Jan 19, 2015

One reason Chrome may not send If-None-Match is when the response includes an "HTTP/1.0" instead of an "HTTP/1.1" status line. Some servers, such as Django's development server, send an older header (probably because they do not support keep-alive) and when they do so, ETags don't work in Chrome.

Sample HTTP/1.0 server response source

In the "Response Headers" section, click "view source" instead of the parsed version. The first line will probably read something like HTTP/1.1 200 OK — if it says HTTP/1.0 200 OK Chrome seems to ignore any ETag header and won't use it the next load of this resource.

There may be other reasons too (e.g. make sure your ETag header value is sent inside quotes), but in my case I eliminated all other variables and this is the one that mattered.

UPDATE: looking at your screenshots, it seems this is exactly the case (HTTP/1.0 server from Python) for you too!

Assuming you are using Django, put the following hack in your local settings file, otherwise you'll have to add an actual HTTP/1.1 proxy in between you and the ./manage.py runserver daemon. This workaround monkey patches the key WSGI class used internally by Django to make it send a more useful status line:

# HACK: without HTTP/1.1, Chrome ignores certain cache headers during development!
#       see https://stackoverflow.com/a/28033770/179583 for a bit more discussion.
from wsgiref import simple_server
simple_server.ServerHandler.http_version = "1.1"