Alamofire: 304 HTTP Status code - ignore cached response / handle manually

Created on 27 Jul 2015  ·  10Comments  ·  Source: Alamofire/Alamofire

Hi,

I have the following problem:

We fire requests against a sync api. If we have any server changes we get a 200 with the according json which includes the modified entities.
If we have no changes we correctly get a 304 from the sever, but Alamofire caches the latest 200 response and provides it instead. So it is impossible to know if we got a 304 (because that would mean we don't have to do anything).

We could disable the cache, but that would mean we lose the 304 caching convenience for all the other requests that have nothing to do with syncing.

Recommendation: Maybe it would be useful to manually disable/enable the cache for every request (is that somehow possible?) or to provide something like 'original status code'.
That are just the first thoughts, maybe you come up with a better solution.

Or did I miss something?

question

Most helpful comment

In my case it worked like this:

let headers: HTTPHeaders = [ "Some-Header": "value"]
let route: URL = URL(string: "https://..")!
let parameters: Parameters = ["name": "value"]

var originalRequest = try! URLRequest(url: route, method: .post, headers: headers)

// This is the important line.
originalRequest?.cachePolicy = .reloadIgnoringCacheData 

let encodedURLRequest = try! URLEncoding.default.encode(originalRequest, with: parameters)

Alamofire.request(encodedURLRequest).responseJSON { response in
    // Handle stuff...
}

All 10 comments

Great question...totally possible already. You need to use the URLRequestConvertible in combination with an NSMutableURLRequest to override the cachePolicy for that particular request. Check out the documentation and you'll see what I mean.

Here's an example of a test that creates an NSMutableURLRequest and customizes it before starting a Request with it.

Cheers 🍻

Hey cnoon! Thx for the reply!

So I missed something ;)
Thanks for the hint. I already had to build a custom URL Request because this is the only way to send a JSON array in a post body. So it was only one line for the cachePolicy ;)

Sorry, stackoverflow would've been the better place for this question.

Facing a similar problem, even the server returns 304, the NSCachedURLResponse is always 200 and hide the "original status code" since the whole caching is handled under the hood.

So you can save that cache anyways, and while providing the correct If-Modified-Since or If-None-Match values in the next request, iOS will still send the request, get a 304 from server, and then give you a 200 NSCachedURLResponse again

Ref: me (I used a proxy to observe the 304) + this guy: http://andrewmarinov.com/ioss-corenetwork-lying/

hi @cnoon https://github.com/Alamofire/Alamofire/blob/swift-2.0/Tests/ValidationTests.swift#L238-L260 is already down. Can share here how to disable caching for GET request?

I'd appreciate it as well

Please, i would really like a good example to solve this issue.

It looks like they probably moved these test to Tests/CacheTests.swift

In my case it worked like this:

let headers: HTTPHeaders = [ "Some-Header": "value"]
let route: URL = URL(string: "https://..")!
let parameters: Parameters = ["name": "value"]

var originalRequest = try! URLRequest(url: route, method: .post, headers: headers)

// This is the important line.
originalRequest?.cachePolicy = .reloadIgnoringCacheData 

let encodedURLRequest = try! URLEncoding.default.encode(originalRequest, with: parameters)

Alamofire.request(encodedURLRequest).responseJSON { response in
    // Handle stuff...
}

@r-dent how can that work? Then the response code will always be 200, right?

@haemi Yes, i guess. But i´m not in the topic right now. You should test it yourself to be sure.

Was this page helpful?
0 / 5 - 0 ratings