Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eTag support using Alamofire 4

I am trying to enable eTag support in my app. I am using Alamofire 4 in my swift 3 project.

It seems that eTag is transparently handled by URLRequest which Alamofire uses:

NSURLCache and ETags

But it doesn't work.

Here is http header sent by web server:

headers {
    Connection = "keep-alive";
    "Content-Length" = 47152;
    "Content-Type" = "application/json";
    Date = "Tue, 06 Dec 2016 22:43:18 GMT";
    Etag = "\"ecf38288be2f23f6710cafeb1f344b8c\"";
} })

Do yo have any hint?

Thanks a lot.

like image 422
thierryb Avatar asked Dec 06 '16 22:12

thierryb


2 Answers

By default, caching is ON. If you log HTTP traffic inside your app, you might see cached responses, without the app making requests to server this time.

In case when URLSession decides to return cached response instead of going to server you will see same Date HTTP response header.

To ensure caching is working, you should inspect network packets between your app and server.

like image 153
paiv Avatar answered Oct 22 '22 03:10

paiv


This is the default cache policy when using an URLRequest.

If you want to change this behavior and see the "real" response from the network, so that you can handle the eTag and the statusCode by yourself, you just need to change the cache policy to reloadIgnoringCacheData:

var exampleRequest = try! URLRequest(url: route, method: .post, headers: headers)
// This is the important line.
exampleRequest?.cachePolicy = .reloadIgnoringCacheData

Hope this helps someone!

like image 40
Davide Castello Avatar answered Oct 22 '22 04:10

Davide Castello