I’m implementing AFHTTPClient as a singleton class, as recommended in the docs, and calling that with JSON data in a post, and receiving JSON data in return:
[[BMNetworkCalls sharedInstance] postPath:theURL parameters:theDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"my return is: %@", [responseObject valueForKeyPath:@"Result"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error in network call: %@", [error localizedDescription]);
}];
All fine and good, but if I receive an error, (“error in HTTPRequestOperation: Expected status code in (200-299), got 400”), I actually want to read the responseObject here as well (this is the way the API I am using tells me what class of error I caused).
I could do that using AFJSONRequestOperation:
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"my return is: %@", [JSON valueForKeyPath:@"Result"]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"exception code: %@", [JSON valueForKeyPath:@"ExceptionCode"]);
NSLog(@"exception message: %@", [JSON valueForKeyPath:@"ExceptionMessage"]);
}];
[operation start];
How can I (and can I?) do it using AFHTTPClient?
The
operationvariable has everything that you need: