4

我正在请求一个 JSON-API,如下所示:

     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
    }

由于服务器进行缓存(我通过 CharlesProxy 验证),我希望状态码 304 但记录了 200 - 有没有办法获得“真实”状态码?

4

1 回答 1

2

将 cachePolicy 设置为 .reloadIgnoringCacheData 对我有用。

request.cachePolicy = .reloadIgnoringCacheData

于 2017-12-15T21:00:33.520 回答