Chaudhry Talha
Chaudhry Talha

Reputation: 7888

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.

Upvotes: 5

Views: 2105

Answers (2)

Ekta Padaliya
Ekta Padaliya

Reputation: 5799

try to use below code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

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

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

Upvotes: 3

KKRocks
KKRocks

Reputation: 8322

Try following code using Afnetworking 3.0

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        self.responseHandlers(YES,responseObject);
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        self.responseHandlers(NO,nil);
    }];

Upvotes: 0

Related Questions