AFJSONRequestOperation hitting failed block on 200 response.statusCode

238 views Asked by At

My AFJSONRequestOperation is hitting the failure block on a 200 response. Is this because I have additional JSON?

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *d = (NSDictionary *)responseObject;
    bool required = [d[@"payment_required"] boolValue];
    [self.delegate paymentRequired:required];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    int statusCode = operation.response.statusCode;
    NSLog(@"status code: %d response: %@", statusCode, operation.responseString);
    if (operation.response.statusCode == 402) {
        [self.delegate paymentRequired:true];
        return ;
    }
    [self handleOperationFailed:operation action:^{
        [self determinePaymentRequired];
    }];
}];

yields in console

status code: 200 response: {'payment_required':'false'}

Why is this happening?

2

There are 2 answers

0
Marcelo On BEST ANSWER

Your JSON is not valid. It has single quotes ({'payment_required':'false'}), but JSON format needs them to be double ones:

{
     "payment_required" : false 
}

PS: I also removed quotes on false, because false is a valid value (and it's preferred).

0
HunTer DRF On

Based on my experience, there are 3 condition you need to meet to hit the success block:

  1. 200 response code
  2. a proper JSON object included in the response
  3. the Content-Type of the response is set to application/json

Hope this can help you.