I'm getting some very strange behavior using AFNetworking 2.0 to access the Github API. The first time I make an API request to this end point http://developer.github.com/v3/oauth/#get-or-create-an-authorization-for-a-specific-app to get the OAuth token, it comes back with a 200 or 201 response like it should. If I quit the application and try again, the request comes back with a 412 response, even though the requests are identical. I have not been able to reproduce this using curl. I'll paste the requests and responses here.
Request response 1 that passes
PUT 'https://api.github.com/authorizations/clients/a162f34fd261c382b59d': {
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
Authorization = "Basic ****************************";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Staticly/1.0 (iPad Simulator; iOS 7.0.3; Scale/1.00)";
} {"client_secret":"*****************************","scope":["repo"]}
2014-01-08 10:43:26.313 Staticly[6035:70b] PUT 'https://api.github.com/authorizations/clients/a162f34fd261c382b59d': {
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
Authorization = "Basic **************************";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Staticly/1.0 (iPad Simulator; iOS 7.0.3; Scale/1.00)";
} {"client_secret":"*******************************","scope":["repo"]}
2014-01-08 10:43:26.364 Staticly[6035:70b] 200 'https://api.github.com/authorizations/clients/a162f34fd261c382b59d' [0.0510 s]: {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval";
"Cache-Control" = "private, max-age=60, s-maxage=60";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 08 Jan 2014 15:43:24 GMT";
Etag = "\"257ff8977266fd375142f862e0536b53\"";
Server = "GitHub.com";
Status = "200 OK";
"Transfer-Encoding" = Identity;
Vary = "Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding";
"X-Content-Type-Options" = nosniff;
"X-GitHub-Media-Type" = "github.beta";
"X-GitHub-Request-Id" = "633B6CDC:6F7C:9F9595F:52CD721C";
"X-RateLimit-Limit" = 5000;
"X-RateLimit-Remaining" = 4985;
"X-RateLimit-Reset" = 1389198563;
} (null)
Request response 2 that fails
PUT 'https://api.github.com/authorizations/clients/a162f34fd261c382b59d': {
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
Authorization = "Basic *********************";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Staticly/1.0 (iPad Simulator; iOS 7.0.3; Scale/1.00)";
} {"client_secret":"************************","scope":["repo"]}
2014-01-08 10:45:39.187 Staticly[6078:70b] PUT 'https://api.github.com/authorizations/clients/a162f34fd261c382b59d': {
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
Authorization = "Basic ***************************";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Staticly/1.0 (iPad Simulator; iOS 7.0.3; Scale/1.00)";
} {"client_secret":"*************************","scope":["repo"]}
2014-01-08 10:45:39.188 Staticly[6078:70b] 412 'https://api.github.com/authorizations/clients/a162f34fd261c382b59d' [0.0018 s]: {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval";
"Cache-Control" = "private, max-age=60, s-maxage=60";
"Content-Type" = "text/html;charset=utf-8";
Date = "Wed, 08 Jan 2014 15:45:37 GMT";
Etag = "\"257ff8977266fd375142f862e0536b53\"";
Server = "GitHub.com";
Status = "412 Precondition Failed";
"Transfer-Encoding" = Identity;
"X-Content-Type-Options" = nosniff;
"X-GitHub-Request-Id" = "633B6CDC:6F7A:91EF162:52CD72A1";
"X-RateLimit-Limit" = 5000;
"X-RateLimit-Remaining" = 4983;
"X-RateLimit-Reset" = 1389198563;
} (null)
This is not an issue with AFNetworking.
If you have questions about using the GitHub API, talk to GitHub, or take this question over to Stack Overflow, or another appropriate forum.
I get the feeling that this has to do with something AFNetworking is doing, because it is not reproducible using anything but AFNetworking. Curl doesn't exhibit this behavior.
It's not AFNetworking. If anything, it's NSURLConnection or NSURLSession.
There's also a good chance that you're not actually replicating behavior correctly with curl.
Finally figured this out, just in case anyone is looking for the answer. It was an issue with the cache policy in my AFHTTPSessionManager. The defaultSessionConfiguration wasn't working for me.
Hi @bringel How you solve this? I also got 412 with AFNetworking.
Most helpful comment
Finally figured this out, just in case anyone is looking for the answer. It was an issue with the cache policy in my AFHTTPSessionManager. The defaultSessionConfiguration wasn't working for me.