is there an easy way to get the http status code in the failure block from AFHTTPClient?
Ok, found the answer with the operation object
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"error code %d",[operation.response statusCode]);
}];
In newer versions of AFNetworking, you can retrieve the response object from the error:
[[[error userInfo] objectForKey:AFNetworkingOperationFailingURLResponseErrorKey] statusCode]
This is handy if you're doing error handling further up the line and don't want to pass around the response object.
For AFNetworking 3.0, use
failure:^(NSURLSessionTask *operation, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)operation.response;
httpResponse.statusCode;
NSLog(@"status code: %li", (long)httpResponse.statusCode);
}
NSInteger operationStatusCode = [operation.error code];
NSInteger httpStatusCode = operation.response.statusCode;
If the requests were cancelled/unreachable/timeout, httpStatusCode
will be always 0
.
Alternatively you can identify the issue by understanding the operationStatusCode
. It is a NSError
Object.
- If it cannot reach/timeout/no network to process request, the
operationStatusCode
will be-1009
. - If you cancel the operations queue the
operationStatusCode
will be-999
.
You can check all other NSError
codes and their descriptions in Apple's documentation