how to get responseString when JSON failed with AFJSONRequestOperation

287 views Asked by At

I'm having difficulties to get responseString when server failed to send a valid JSON response (fe. php echos some temp variable or something went wrong). I am using AFJSONRequestOperation from AFNetwoking like this:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) {
        NSLog(@"object: %@", responseObject);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseString) {
        NSLog(@"failure: %@", responseString);
    }
];

responseString is case of failure is always nil. When I tried to read the documentation (http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFJSONRequestOperation.html#//api/name/JSONRequestOperationWithRequest:success:failure:) I've found that there is written that failure gets three arguments (but in reality four, the fourth being always nil). Is there any simple way to get the response as a string in that case?

1

There are 1 answers

5
mattt On

If responseString is nil, then you either didn't receive any data from the server, or the data could not be used to create a valid NSString object.

By the sound of your Cocoa error 3840, which corresponds to an NSJSONSerialization error, my guess is that the server was indeed sending back an empty response.