Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome is not sending if-none-match

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.

like image 977
avances123 Avatar asked Mar 29 '14 16:03

avances123


3 Answers

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"
like image 97
natevw Avatar answered Sep 24 '22 14:09

natevw


Also check that caching is not disabled in the browser, as is often done when developing a web site so you always see the latest content.

like image 20
Jack Desert Avatar answered Sep 24 '22 14:09

Jack Desert


I had a similar problem in Chrome, I was using http://localhost:9000 for development (which didn't use If-None-Match).

By switching to http://127.0.0.1:9000 Chrome1 automatically started sending the If-None-Match header in requests again.

Additionally - ensure Devtools > Network > Disable Cache [ ] is unchecked.

1 I can't find anywhere this is documented - I'm assuming Chrome was responsible for this logic.

like image 41
Nick Grealy Avatar answered Sep 23 '22 14:09

Nick Grealy