Search code examples
iosobjective-cafnetworkingafjsonrequestoperation

AFJSONRequestOperation hitting failed block on 200 response.statusCode


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?


Solution

  • 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).