Search code examples
afnetworking-3

Not able to parse response in JSON response


I am using AFNetworking 3, Xcode 7.2 for iOS 9.

AFURLSessionManager *ses = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
ses.responseSerializer = [AFJSONResponseSerializer serializer];

//Request
[[AFJSONRequestSerializer serializer] setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"GET" URLString:endPoint parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [ses dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];
[dataTask resume];

but I am getting error response as:

Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain"

The request content type is still set to "text/plain" I am not sure what I am missing .


Solution

  • I used http response serializer itself and it worked fine.

    AFURLSessionManager *ses = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        ses.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSMutableDictionary * innerJson = [NSJSONSerialization
                                                   JSONObjectWithData:responseObject options:kNilOptions error:&error1];