Swift: URLSession.shared.dataTask says status code 304 = 200?

人走茶凉 提交于 2019-12-11 03:17:18

问题


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?


回答1:


Setting the cachePolicy to .reloadIgnoringCacheData worked for me.

request.cachePolicy = .reloadIgnoringCacheData



来源:https://stackoverflow.com/questions/46696624/swift-urlsession-shared-datatask-says-status-code-304-200

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!