5
votes

AFNetworking response failure block is being called when I get status code 200. How can I make the success be called instead?

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://128.199.94.58/test/bt/client_token.php" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         self.clientToken = responseObject[@"customerID"];
         NSLog(@"Client Token received.");
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // Handle failure communicating with your server
         NSLog(@"Client Token request failed.%@",operation.responseString);
         NSLog(@"error code %ld",(long)[operation.response statusCode]);
     }];
5

5 Answers

5
votes

Look at the value of error. It will tell you why the connection failed. "Failure" in this context has nothing to do with the status code. Returning "404" is still a "success." Failure means you were unable to complete the operation.

2
votes

use acceptableStatusCodes as follows:

 AFHTTPRequestOperationManager *manager =  [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [TimeoutAFJSONRequestSerializer serializer];
NSMutableIndexSet* codes = [[NSMutableIndexSet alloc] init];
[codes addIndex: 200];
manager.responseSerializer.acceptableStatusCodes = codes;

[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
 {


 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
 }];
1
votes

I run this code and it work find.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"http://128.199.94.58/test/bt/client_token.php" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                              options:kNilOptions
                                                                error:nil];
         self.clientToken = json[@"customerID"];
         NSLog(@"Client Token received.");
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // Handle failure communicating with your server
         NSLog(@"Client Token request failed.%@",operation.responseString);
         NSLog(@"error code %ld",(long)[operation.response statusCode]);
     }];

responce is:

json:
{
    customerID = "eyJ2ZXJzaW9uIjoyLCJhdXRob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lLmNvbS90b3MiLCJiYXNlVXJsIjoiaHR0cHM6Ly9hc3NldHMuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhc3NldHNVcmwiOiJodHRwczovL2NoZWNrb3V0LnBheXBhbC5jb20iLCJkaXJlY3RCYXNlVXJsIjpudWxsLCJhbGxvd0h0dHAiOnRydWUsImVudmlyb25tZW50Tm9OZXR3b3JrIjp0cnVlLCJlbnZpcm9ubWVudCI6Im9mZmxpbmUiLCJ1bnZldHRlZE1lcmNoYW50IjpmYWxzZSwiYnJhaW50cmVlQ2xpZW50SWQiOiJtYXN0ZXJjbGllbnQzIiwibWVyY2hhbnRBY2NvdW50SWQiOiI2ejl3eGtkanlyNnQzbmg1IiwiY3VycmVuY3lJc29Db2RlIjoiVVNEIn0sImNvaW5iYXNlRW5hYmxlZCI6ZmFsc2UsIm1lcmNoYW50SWQiOiJ6cWQ5ZHBqZmZkc2s3eG55IiwidmVubW8iOiJvZmYifQ==";
}

It may be work for you.

0
votes

If you check your error in failure block it clearly say that invalid content type. You need to set the content type of the manager as follows

manager.requestSerializer = [AFJSONRequestSerializer serializer];
0
votes

try this

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

and in success block

 success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:manager.responseData options:kNilOptions error:nil];
     self.clientToken = dic[@"customerID"];
     NSLog(@"Client Token received.");
 }