5

I'm requesting a JSON-API like the following:

     var request = URLRequest(url: url)
    request.httpMethod = "GET"
    request.addValue(CredentialsProvider.shared.credentials, forHTTPHeaderField: "Authorization")

    let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
        guard let data = data, let _ = response, error == nil else {
            return
        }

        let response1 = response as! HTTPURLResponse
        print(response1.statusCode) // 200 instead of 304
    }

As the server does caching (which I verified via CharlesProxy) I would expect status code 304 but 200 is logged - is there a way to get the "real" status code?

swalkner
  • 16,679
  • 31
  • 123
  • 210
  • You're positive your back-end is specifying a status ode 304 in your desired use-case? – jlmurph Oct 11 '17 at 20:38
  • @murphguy yes, as it is said by CharlesProxy – swalkner Oct 11 '17 at 20:39
  • Don't you also need to pass in `If-Modified-Since` into the header so that it becomes a conditional get request as oppose to a non conditional get request you have there – TNguyen Oct 11 '17 at 21:01
  • 1
    try this `request.cachePolicy = .reloadIgnoringCacheData` – Vini App Oct 11 '17 at 21:42
  • It should be a `"POST"` not a `"GET"`. If you just need to download there is no need to use a `URLRequest` unless you are gonna fetch just the header. Btw `let _ = response` is pointless – Leo Dabus Oct 11 '17 at 21:43
  • @LeoDabus `let _ = response` will cause the closure to return if the response var is nil. – ekreloff Oct 11 '17 at 22:08
  • Still pointless you have the error to check. If he wants to unwrap it just use ``guard let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200, let data = data, error == nil`` – Leo Dabus Oct 11 '17 at 22:14
  • @LeoDabus why would it be a `"POST"` instead of a `"GET"` if he's expecting a status code of `304` that doesn't make any sense at all. A 304 is returned on a conditional get request only. – TNguyen Oct 11 '17 at 22:27
  • I found [this comment](https://stackoverflow.com/questions/31551964/how-to-use-nsurlsession-to-determine-if-resource-has-changed#comment51107181_31567943) on an [answer to a similar problem](https://stackoverflow.com/a/31567943/6740382) which might be of interest. **TL;DR: try a `downloadTask` instead of `dataTask`.** – caseynolan Oct 12 '17 at 03:06
  • @JackDaw didn't help, CharlesProxy again says `304`, in the app I see `200`. – swalkner Oct 12 '17 at 07:34
  • @TNguyen why should I need this? The server already returns `304`, I only don't see it in the app... – swalkner Oct 12 '17 at 07:34
  • @LeoDabus You could get an empty response and thus a nil response variable without having an error. – ekreloff Oct 12 '17 at 17:41

1 Answers1

6

Setting the cachePolicy to .reloadIgnoringCacheData worked for me.

request.cachePolicy = .reloadIgnoringCacheData

jmprice
  • 61
  • 1
  • 3
  • Which means you are ignoring the Akamai/local cached response, which is not good in network/api point of view. – Naveen Shan Sep 23 '19 at 03:27
  • 1
    This is the correct solution IMO. If your goal is that your app deals with HTTP 304 programmatically then this solution will let 304 pass through. Otherwise NSUrlCache would fake return HTTP 200 and content from local cache. – adamsfamily Aug 26 '22 at 11:44