Nitish
Nitish

Reputation: 14123

AFNetworking - AFHTTPRequestOperationManager does not return data

I am using AFHTTPRequestOperationManager for login.

- (IBAction)loginAction
{
    [TGProjectHandler clearCookies];

    NSDictionary *params = @{@"Email": _email.text,
                             @"Password": _password.text,
                             @"IsRemember": @"true",
                             @"ReturnUrl": @"",
                             @"UserId": @0,
                             @"OutResponse": @0};

    [_sharedHandler.requestManager POST:TGURL_LOGIN
                             parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

     NSError *e;
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&e];                                    
      NSLog(@"%@\n\n%@", jsonDict, responseObject);
      NSString *outResponse = responseObject[@"Object"][@"OutResponse"];
      if (outResponse.integerValue == 1){
         NSLog(@"login successful: %@", outResponse);
         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"User_Logged_In"];
         [TGProjectHandler saveCookiesToDefaults];
         [self performSegueWithIdentifier:@"HomePage" sender:self];
       }else
        {
           [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:@"Invalid credentials" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        }
     }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [TGProjectHandler dismissHUD];
         [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
       }];
}

The service function returns UserID, but this is what I am getting in response while NSLog.

NSLog(@"%@\n\n%@", jsonDict, responseObject); 



Object =     {
        OutResponse = 1;
        ReturnUrl = "<null>";
    };
    Success = 1;
}

{
    Object =     {
        OutResponse = 1;
        ReturnUrl = "<null>";
    };
    Success = 1;
}  

Why is UserId not coming in response?

Upvotes: 1

Views: 476

Answers (1)

Pratik Mistry
Pratik Mistry

Reputation: 2945

By looking at your code I guess the problem might be content type in your request. Check if your content type is set properly.

For Example -

[_sharedHandler.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

Also you can change response serializer to AFJSONResponseSerializerwhich will automatically convert your response to dictionary or array.

_sharedHandler.responseSerializer = [AFJSONResponseSerializer serializer];

Upvotes: 1

Related Questions