Search code examples
iosobjective-cjsonnsurl

AFHTTPSession Failure : The data couldn’t be read because it isn’t in the correct format


I am new to IOS and Objective C. I am trying to post values to URl and get response using AFHTTPSessionManager. But iam getting the the error AFHTTPSession Failure : The data couldn’t be read because it isn’t in the correct format.

Here is my code

- (void)submitForgetPasswordRequest:(NSString *)email {

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    NSDictionary *dict = @{@"Email": email};
  //  NSLog(@"Login dicxtionary : %@", dict);

    MBProgressHUD *hud;
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"Please wait. Logging in...";
    [hud showAnimated:YES];

    // send user login data to hosting via AFHTTP Async Request in AFNetworking
    [manager POST:BASEURL parameters:dict success:^(NSURLSessionTask *task, NSString* responseObject) {

        [hud hideAnimated:YES];
        // Login response validation
        NSLog(@"Raw response object : %@", responseObject);
        if ([responseObject isEqualToString:@""]) {
            [self showErrorMessage:@"Request failed" Message:@"Please try again later!"];
        }else {
           // NSError *error = nil;
            //NSLog(@"response type : %@", NSStringFromClass([responseObject class]));
           // NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
            NSLog(@"Response password Reset : %@", responseObject);
            [self showErrorMessage:@"Success" Message:@"A password reset link has been send to your email address."];
        }
    } failure:^(NSURLSessionTask *task, NSError *error) {
        NSLog(@"AFHTTPSession Failure : %@", [error localizedDescription]);
    }];

}

can someone help me to fix this. tnx.


Solution

  • you can use this line also and then check

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
       manager.responseSerializer = [AFHTTPResponseSerializer serializer];