Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 3.0 The data couldn’t be read because it isn’t in the correct format

There are other questions with similar titles but none of them helped me. I've to send a PUT request to server in order to change the status of appointment so I've made this method -(void)appointmentStatusChangedTo:(NSString *)statusID atAppointmentID:(NSString *)appointmentID In which I'm setting the URL and Parameters as

NSString *string = [NSString stringWithFormat:@"%@/API/Appointments/3",BaseURLString];
    NSDictionary *para = @{
                           @"AppointmentStatusId":statusID,
                           @"ID":appointmentID
                           };

Then I've made URL request as

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"PUT" URLString:string parameters:para error:nil];

After that I'm setting the header for an authorization token as

 NSString *token = [NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"]];

    [req setValue:token forHTTPHeaderField:@"Authorization"];

So finally I'm calling it as

[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
        if (!error) {
            if (response) {
                NSLog(@"Respose Object: %@",responseObject);
                [self.patientsAppointmentsTableView reloadData];
            }
        }
        else {
    // NSLog(@"Error: %@, %@, %@", error, response, responseObject);
    NSLog(@"Error: %@", error.localizedDescription);
}
        }] resume];

Now it is successfully sending the data to the server but as a response I'm getting

Error: The data couldn’t be read because it isn’t in the correct format.

I am not sure what the response might look like at the moment as I'm not in contact with backend guy. But as far as I remember it was just a simple 1. SO kindly tell me how to handle any type of response using AFNetworking 3.0 or any change in my code.

like image 308
Chaudhry Talha Avatar asked Oct 19 '22 00:10

Chaudhry Talha


1 Answers

try to use below code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];

manager.requestSerializer=serializer;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
like image 110
Ekta Padaliya Avatar answered Oct 29 '22 18:10

Ekta Padaliya