Is it possible to ignore cache-control
headers when performing a request/handling the response with AlamoFire?
Currently I am making a request as follows, and the server is returning large cache-control
headers, when in fact we need to ignore them.
Alamofire.request(.GET, url).responseJSON { (_, _, result) in // Do something
I know the correct solution is to modify the server response, but this is not feasible at this time.
Also, there are other requests where I do want to honor the cache-control
headers, so ideally I would a solution that does not involve changing a global configuration of AlamoFire.
The Cache-Control HTTP header holds directives (instructions) for caching in both requests and responses. A given directive in a request does not mean the same directive should be in the response. A given directive in a request does not mean the same directive should be in the response.
Cache-Control: no-store The no-store directive will prevent a new resource being cached, but it will not prevent the cache from responding with a non-stale resource that was cached as the result of an earlier request.
The response may be stored by any cache, even if the response is normally non-cacheable. The response may be stored only by a browser's cache, even if the response is normally non-cacheable. If you mean to not store the response in any cache, use no-store instead. This directive is not effective in preventing caches from storing your response.
The Cache-Control HTTP header holds directives (instructions) for caching in both requests and responses. A given directive in a request does not mean the same directive should be in the response. Header type. Request header, Response header. Forbidden header name.
To ignore the cached data, you need to set the cachePolicy
on the NSURLRequest
before using Alamofire to start it.
let URL = NSURL(string: "https://my_url_path...")!
let URLRequest = NSMutableURLRequest(URL: URL)
URLRequest.cachePolicy = .ReloadIgnoringCacheData
Alamofire.request(URLRequest)
.response { response in
print(response)
}
Setting the
cachePolicy
on the URL request always overrides the value set on theNSURLSessionConfiguration
.
By default, the NSURLSessionConfiguration cache policy is set to .UseProtocolCachePolicy
which will honor the Cache-Control
header values.
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