Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If-None-Match and NSURLConnection

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) ?

like image 431
fvisticot Avatar asked Jun 30 '13 10:06

fvisticot


People also ask

What does if-none-match mean?

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.

What are the If modified since and if-none-match header used for?

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.

What is ETag in REST API?

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.


1 Answers

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

like image 164
johnny kehr Avatar answered Sep 30 '22 09:09

johnny kehr