Mackey18 Mackey18 - 2 years ago 185
iOS Question

AFNetworking 2.0 Get JSON from code 400 in failure block

I'm using

for a
request. Now I'm deliberately entering incorrect information to handle a
error code. Now, the web service actually returns a
with a message explaining to the user what they've done wrong. I would very much like to get this
to display the message in a
. However, the failure block of:

[operationManager POST:ServerURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: Status Code: %d", operation.response.statusCode);
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed: Status Code: %d", operation.response.statusCode);

doesn't pass down a responseObject like the one in the success block. So does anyone know how I can access the JSON returned by the Web Service with the 400 error? The
NSError *error
simply gives me
Request failed: bad request (400)
and not the JSON returned.

Any help would be appreciated,

Answer Source

Looking at the code for - responseObject, it appears that an HTTP error prevents it from being populated. You can grab the responseData directly and parse it yourself, but I would say this is either a bug or a good enhancement request. (It looks like - responseObject probably should be checking self.responseSerializationError, not self.error, when deciding if it should try to build a response object.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download