问题
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