Response failure when using AFHTTPRequestOperationManager
Your hitting your failure block because the api your hitting is returning an http response code of 400 and AFNetworking is correctly interpreting that as an error. You can still get the more detailed data from the response by using the responseData
and responseString
properties of the AFHTTPRequestOperation object provided in the failure block.
2vision2
Updated on June 08, 2022Comments
-
2vision2 almost 2 years
When i tried to call API(AFHTTPRequestOperationManager) for authentication method with exact username and password getting exact response and method was success. Then i tried with invalid username and password I'm getting response is failure.
i tried ASIHTTPRequest API for same method its working fine. So please check below request and response and advice me how to resolve this issues. But in AFHTTPRequestOperationManager invalid user response is failure.
ASIHTTPRequest CALL sample:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[ NSURL URLWithString:[SharedUtil URLByKey:@"A_URL"]]]; request.shouldAttemptPersistentConnection=NO; [request setRequestMethod:@"POST"]; [request addRequestHeader:@"enctype" value:@"application/x-www-form-urlencoded"]; [request addRequestHeader:@"Authorization" value:[SharedUtil authorizationHeader ]]; [request addRequestHeader:@"Accept" value:@"application/json"]; [request setValidatesSecureCertificate:NO]; [request setPostValue:@"pa" forKey:@"type"]; [request setPostValue:username forKey:@"username"]; [request setPostValue:password forKey:@"password"]; [request setDelegate:self]; [request setTimeOutSeconds:120]; [self setTimer]; [request setDidFinishSelector: @selector(responseAuthUserSuccess:)]; [request setDidFailSelector: @selector(responseAuthUserFailed:)]; [post release]; // Cancels an asynchronous request [networkQueue addOperation: request];
Response is ASIHTTPRequest and method is success
{"error":"invalid_grant","error_description":"Unable to validate user [email protected]"}
Using AFHTTPRequestOperationManager CALL sample:
AFHTTPRequestOperationManager *managerAF = [AFHTTPRequestOperationManager manager]; NSDictionary *params = @{ @"type": @"pa", @"username": username, @"password": password }; [managerAF.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [managerAF.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"enctype"]; [managerAF.requestSerializer setValue:[SharedUtil authorizationHeader] forHTTPHeaderField:@"Authorization"]; managerAF.responseSerializer = [AFJSONResponseSerializer serializer]; managerAF.securityPolicy.allowInvalidCertificates = YES; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [managerAF POST:[SharedUtil URLByKey:@"A_URL"] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSData* data=[operation.responseString dataUsingEncoding:NSUTF8StringEncoding]; } [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); [self stopTimer]; }];
AFHTTPRequestOperationManager Response and method is failure :
Printing description of error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0xd3b7670 {NSErrorFailingURLKey=https://example.com/token, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0xd29a010> { URL: https://example.com/token } { status code: 400, headers { "Cache-Control" = "no-store"; Connection = "keep-alive"; "Content-Type" = "application/json;charset=UTF-8"; Date = "Fri, 25 Jul 2014 05:15:19 GMT"; Pragma = "no-cache"; Server = "nginx/1.6.0"; "Transfer-Encoding" = Identity; "X-Powered-By" = "Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)"; } }, NSLocalizedDescription=Request failed: bad request (400)}
-
2vision2 almost 10 yearsBrandon Roth : thanks for your answer. I'm getting response from responseString
-
2vision2 almost 10 yearsBrandon Roth : one more help when i run in device got ld: library not found for -lPods clang: error: linker command failed with exit code 1 (use -v to see invocation)
-
Brandon almost 10 yearsThe lPods problem is an issue with cocoapods. Google around and you should be able to find the solution.