My server is setting the etag header element for caching support.
iOS (6.1.4) application is using the native NSURLConnection class to send XML request to the server
first time the server is sending the response with the etag set in the header
If the iOS app is sending exactly the same request to the server, I can see in the server logs that the if-none-match header is not filled by the NSURLConnection
... and then the server is responding with 200 instead of 304
Cache policy used for the request is:
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
NSURLCache is initialized with
[[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024*10];
My questions: - Is it normal that NSURLConnection did not set the "if-none-match" header field ? - Do I need to set this header field by myself ? (getting response from cache, reading the etag value and setting in in the request header) ?
The If-None-Match HTTP request header makes the request conditional. For GET and HEAD methods, the server will return the requested resource, with a 200 status, only if it doesn't have an ETag matching the given ones.
The If-Modified-Since header is used to specify the time at which the browser last received the requested resource. The If-None-Match header is used to specify the entity tag that the server issued with the requested resource when it was last received.
An entity tag, or ETag, is a mechanism that is provided by the HTTP protocol so that a browser client or a script can make conditional REST requests for optimistic updating or optimized retrieval of entities.
I was running into the same issue with this. For me, I was using the default cache policy (NSURLRequestUseProtocolCachePolicy) and it was automatically setting the "if-none-match" header field which the server would check for. However, setting the cache policy as "NSURLRequestReloadIgnoringLocalCacheData" obviously removed that header field.
Have you tried the other cache policy values to see if they add that header for you?
A great blog on these different cache policy values can be seen here: http://nshipster.com/nsurlcache/
edit: I found another stack overflow question that confirms what I said above: NSURLCache and ETags
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With