0

Is there any way that I can print/NSLOG the string or the array that is being returned by the API in this following login code of mine:

-(void)loginToAPI:(NSString *)email password:(NSString *)password {

    NSString *controller = @"login";
    NSString *action =  @"authenticate";

    NSDictionary *params = @{@"username": email,
                             @"userpass": password,
                             @"controller":  controller,
                             @"action": action,
                             @"app_id": APP_ID,
                             @"app_key": APP_KEY};
    NSLog(@"params %@", params);

    if ([self isNetworkAvailable]) {
        AFHTTPRequestOperationManager *client = [AFHTTPRequestOperationManager manager];
        client.responseSerializer.acceptableContentTypes = [client.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        [client POST:[[Config sharedInstance] getAPIURL]
          parameters:params
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSDictionary *jsonObject= responseObject;
                 NSString *status = [jsonObject objectForKey:@"status"];
                 NSLog(@"Request Successful, response '%@'", jsonObject);
                 if ([status isEqualToString:@"success"]) {
                     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                     NSDictionary *data = [jsonObject objectForKey:@"data"];
                     NSDictionary *userDict = [data objectForKey:@"user"];
                     NSDictionary *djsaDict = [data objectForKey:@"djsa"];

                     User *currentUser = [[User alloc] initWithProperties:userDict];

                     [currentUser save];

                     DJSA_Cutomer *djsa = [[DJSA_Cutomer alloc] initWithProperties:djsaDict];


                     NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:currentUser];
                     NSData *djsaData = [NSKeyedArchiver archivedDataWithRootObject:djsa];

                     [userDefaults setObject:userData forKey:@"currentUser"];
                     [userDefaults setObject:djsaData forKey:@"djsa_customer"];
                     [userDefaults synchronize];

                     [[NSNotificationCenter defaultCenter] postNotificationName: @"LOGIN_SUCCESS" object:currentUser userInfo:nil];

                 } else {
                     [[NSNotificationCenter defaultCenter] postNotificationName: @"LOGIN_FAILED" object:nil userInfo:nil];
                 }
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Request Failed with Error - loginToAPI: %@, %@", error, error.userInfo);
                 [[NSNotificationCenter defaultCenter] postNotificationName: @"SERVER_ERROR" object:nil userInfo:nil];
             }];
    }
}

It is always failing and going to the part "Request Failed with Error - loginToAPI". Is it possible to see the actual values returned by the server so I can diagnose the problem?

Thanks!

1
  • What's the full log of "Request Failed with Error - loginToAPI"? You can hide your url if needed. If I remember correctly AFNetworking encapsulated the errors (and the data) inside it. Commented Oct 10, 2017 at 9:39

3 Answers 3

1

I had the same problem. The solution to it in my case was

client.responseSerializer = [AFHTTPResponseSerializer serializer];

I did that but I was still getting same error. The problem was that I was setting client.requestSerializer instead of client.responseSerializer. Small mistakes but takes lot of time.

Sign up to request clarification or add additional context in comments.

Comments

0

You can get the status code off the response property from the operation object which should give you some idea of what went wrong:

failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Request Failed with Error - loginToAPI: %@, %@", error, error.userInfo);
    NSLog(@"%@", [NSHTTPURLResponse localized​String​For​Status​Code:operation.response.statusCode]);
    [[NSNotificationCenter defaultCenter] postNotificationName: @"SERVER_ERROR" object:nil userInfo:nil];
}];

Otherwise, it's best to use a tool like Charles web proxy to inspect the actual request and response. It has a free trial which should be sufficient to do what you need, and is relatively easy to set up.

Comments

0

Replace this line:

client.responseSerializer.acceptableContentTypes = [client.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

with this:

[client.securityPolicy setValidatesDomainName:NO];
[client.securityPolicy setAllowInvalidCertificates:YES];
client.responseSerializer = [AFHTTPResponseSerializer serializer];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.