Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an easy way to get the http status code in the failure block from AFHTTPClient?

Tags:

afnetworking

I see that there is a list of accepted http status codes that I can modify, but I think it would be cleaner if I can get the http status code in the failure block ..

Ok, found the answer with the operation object

failure:^(AFHTTPRequestOperation *operation, NSError *error){          NSLog(@"error code %d",[operation.response statusCode]); }]; 
like image 722
MonkeyBonkey Avatar asked Dec 12 '11 03:12

MonkeyBonkey


People also ask

How do I retrieve my HTTP status code?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error. Copied!

What is HTTP status code not found?

If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead.

What is HTTP status code1?

We tend to get -1 status codes when there are network issues or connection problems, so we display the user a network problems page in those cases.


Video Answer


2 Answers

Ok, found the answer with the operation object

failure:^(AFHTTPRequestOperation *operation, NSError *error){         NSLog(@"error code %d",[operation.response statusCode]); }]; 
like image 56
MonkeyBonkey Avatar answered Sep 30 '22 15:09

MonkeyBonkey


In newer versions of AFNetworking, you can retrieve the response object from the error:

[[[error userInfo] objectForKey:AFNetworkingOperationFailingURLResponseErrorKey] statusCode] 

This is handy if you're doing error handling further up the line and don't want to pass around the response object.

like image 24
Sam Avatar answered Sep 30 '22 15:09

Sam