0

I need to get the value of Error-Message in the failure block of my request.

Here's the error:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x165151b0 {NSErrorFailingURLKey=http://203.125.112.203:8090/megumi01/api/zipData?lastSynced=1441161682, com.alamofire.serialization.response.error.response= { URL: http://203.125.112.203:8090/megumi01/api/zipData?lastSynced=1441161682 } { status code: 400, headers { "Access-Control-Allow-Headers" = "x-requested-with, Authorization, Content-Type, Accept"; "Access-Control-Allow-Methods" = "POST, GET, OPTIONS, DELETE, PUT"; "Access-Control-Allow-Origin" = "*"; "Access-Control-Max-Age" = 3600; "Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate"; Connection = close; "Content-Type" = "application/json;charset=UTF-8"; Date = "Wed, 02 Sep 2015 10:38:57 GMT"; "Error-Code" = 0808; "Error-Message" = "No updates available"; Expires = 0; Pragma = "no-cache"; Server = "Apache-Coyote/1.1"; "Transfer-Encoding" = Identity; "X-Application-Context" = "application:9090"; "X-Content-Type-Options" = nosniff; "X-Frame-Options" = DENY; "X-XSS-Protection" = "1; mode=block"; } }, NSLocalizedDescription=Request failed: bad request (400)}

I have tried putting "error" into NSData so I could convert it to NSDictionary but it becomes nil.

May I know how could I get the value of the custom header "Error-Message"?

TYIA!

Lie-An
  • 2,563
  • 3
  • 17
  • 20

2 Answers2

0

below code may help you. try it.

NSLog(@"status code  :%ld",(long)[operation.response statusCode]);
NSLog(@"Failure Response %@, %@", error, operation.response.description);
Hardik Shekhat
  • 1,680
  • 12
  • 21
  • In the latest AFNetworking, the responseObject is nil in failure block. That's why we have decided to add a custom header. But I couldn't get the value of the custom header. – Lie-An Sep 03 '15 at 02:01
0

After thinking straightly, I was able to get the proper way to get the custom header in failure block of AFHTTPRequestOperation.

NSDictionary *userInfo = [error userInfo];
NSHTTPURLResponse *alamofireResponse = [userInfo objectForKey:@"com.alamofire.serialization.response.error.response"];
NSDictionary *allHeaders = [alamofireResponse allHeaderFields];
NSString *customMessage = [allHeaders objectForKey:@"Error-Message"];
Lie-An
  • 2,563
  • 3
  • 17
  • 20