Recently I used the GitHub API to request some resources, I found Etag would help me since it could avoid duplicate URL requests. However, I met a problem with HTTP request with ETag included.
According to ETag(https://en.wikipedia.org/wiki/HTTP_ETag), if you want to compare previous Etag with current URL request result, you should send that information within HTTP Header, the key value pair that included in a HTTP Header Field should have the form like this
If-None-Match: "686897696a7c876b7e"
From the above observation, if I got an Etag like this
Etag = "W/\"e1a6465809efe351293dd5bda041a795\""
I should save the part e1a6465809efe351293dd5bda041a795
I then use two kind of code in swift to do a URL request
// components[1]:String = "e1a6465809efe351293dd5bda041a795"
request.setValue("\"\(components[1])\"", forHTTPHeaderField: "If-None-Match")
// components[1]:String = "e1a6465809efe351293dd5bda041a795"
request.setValue("\(components[1])", forHTTPHeaderField: "If-None-Match")
Both of them couldn't work even if Etag hasn't changed.
The result like this:
status code: 200, headers {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval";
"Cache-Control" = "public, max-age=60, s-maxage=60";
"Content-Encoding" = gzip;
"Content-Security-Policy" = "default-src 'none'";
"Content-Type" = "application/json; charset=utf-8";
Date = "Tue, 07 Jul 2015 00:46:53 GMT";
Etag = "W/\"e1a6465809efe351293dd5bda041a795\"";
I am very confused.
If Etag remains the same, the response status should be 304, however every time I request I will get a 200, even if the result has no change.
I think the problem may be caused by the quote representation in swift, any one could help me?
The problem is at my github repo GitPocket, hope someone could help me directly in the repo!! you could find the problem by ctrl + F problem here
After trying with Alamofire, I was able to fix it. I used following request
let URL = NSURL(string: "https://api.github.com/users/jindulys/received_events")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "GET"
//let parameters = ["foo": "bar"]
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.setValue("\"ae651e23bd54274b1c046e7b804feeb7\"", forHTTPHeaderField: "If-None-Match")
return Alamofire.request(mutableURLRequest)
I think the problem only exist in iOS 9.0, I think it is the iOS 9.0 SDK problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With