AlamoFire ignore caching headers

Is it possible to ignore cache-control headers when executing a request / processing a response using AlamoFire?

Currently, I am making a request as follows, and the server returns large cache-control headers, when in fact we need to ignore them.

 Alamofire.request(.GET, url).responseJSON { (_, _, result) in // Do something 

I know that the correct solution is to change the server response, but this is currently not possible.

In addition, there are other requests in which I want to read the cache-control headers, so ideally I would make a decision that is not related to changing the global configuration of AlamoFire.

+5
source share
1 answer

To ignore cached data, you need to set cachePolicy to NSURLRequest before using Alamofire to run 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 cachePolicy in a URL request always overrides the value set in NSURLSessionConfiguration .

By default, the NSURLSessionConfiguration cache policy is set to .UseProtocolCachePolicy , which will respect the values โ€‹โ€‹of the Cache-Control header.

+13
source

Source: https://habr.com/ru/post/1232741/


All Articles