avances123
avances123

Reputation: 2344

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.

Upvotes: 28

Views: 17203

Answers (6)

Taufiq Ahmed
Taufiq Ahmed

Reputation: 747

Chrome will also not send If-Modified-Since and If-None-Match: header if the script/file is downloaded via another script using xmlHttpRequest or jQuery ajax method.

Only those are linked via tag, will add these headers.

Upvotes: 0

enisdenjo
enisdenjo

Reputation: 732

Chrome is not sending the appropriate headers (If-Modified-Since and If-None-Match) because the cache control is not set, forcing the default (which is what you're experiencing). Read more about the cache options here: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache.

You can get the wished behaviour on the server by setting the Cache-Control: no-cache header; or on the browser/client through the Request.cache = 'no-cache' option.

Upvotes: 2

Nick Grealy
Nick Grealy

Reputation: 25902

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.

Upvotes: 8

hgasimov
hgasimov

Reputation: 156

Chrome was not sending 'If-None-Match' header for me either. I didn't have any cache-control headers. I closed the browser, opened it again and it started sending 'If-None-Match' header as expected. So restarting your browser is one more option to check if you have this kind of problem.

Upvotes: 0

Jack Desert
Jack Desert

Reputation: 1303

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.

Upvotes: 23

natevw
natevw

Reputation: 17902

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"

Upvotes: 21

Related Questions