0
votes

I'm making a GET request for JSON using AFNetworking 2.0

    self.managerMasterFetchDeletes = [AFHTTPRequestOperationManager manager];
        self.managerMasterFetchDeletes.requestSerializer.timeoutInterval = 4.0f;
        self.managerMasterFetchDeletes.requestSerializer = [AFJSONRequestSerializer serializer];
        self.managerMasterFetchDeletes.responseSerializer = [AFJSONResponseSerializer serializer];

NSURL *URL = [NSURL URLWithString:rest];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (operation.response.statusCode==200){

            NSDictionary *dictionary   = (NSDictionary*)responseObject;
            NSMutableDictionary *responseData = [NSMutableDictionary new];
            if ([dictionary objectForKey:@"responseData"]) {
                responseData = [[dictionary objectForKey:@"responseData"] mutableCopy];
            }
            [responseData setValue:entity forKey:@"entityName"];
            [responseData setValue:[NSNumber numberWithInt:timeStamp] forKey:@"lastSync"];
            [[NSNotificationCenter defaultCenter]
             postNotificationName:notification
             object:nil userInfo:responseData];

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

 NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:[arrayOperations copy] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
        NSLog(@"%lu of %lu fetch delete complete", numberOfFinishedOperations, totalNumberOfOperations);
    } completionBlock:^(NSArray *operations) {
        NSLog(@"All operations in batch complete");
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"MasterFecthDeleteEnd"
         object:nil
         userInfo:nil];
    }];

    [self.managerMasterFetchDeletes.operationQueue addOperations:operations waitUntilFinished:NO];

But it always returns 403 with this error: unacceptable content-type: text/html

The weird thing is that the same request I do with Cocoa GraphicalHttpClient, it's ok and returns the JSON response and this:

Content-Type: application/json;charset=UTF-8 Date: Fri, 25 Sep 2015 15:28:27 GMT Transfer-Encoding: Identity X-Powered-By: Servlet/2.5 JSP/2.1

Many thanks.

2
the third-party server sends you the forbidden error, not the AFNetworking framework... you may need to request permission from the sysadmin for the content you want to access.holex
Add manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];. hope it works, if it does' not work than can you post your code??Mehul Sojitra
I've tried but no luck.roof

2 Answers

2
votes

It seems like your server send text/html ContentTypes. To accept this type of content add text/html acceptableContentTypes.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];
2
votes

This means that your server is sending "text/html" instead of the already supported types. My solution was to add "text/html" to acceptableContentTypes set in AFURLResponseSerialization class. Just search for "acceptableContentTypes" and add @"text/html" to the set manually.

op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

Of course, the ideal solution is to change the type sent from the server, but for that you will have to talk with the server team.

in you case you can do like that.

NSURL *URL = [NSURL URLWithString:rest];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (operation.response.statusCode==200){

            NSDictionary *dictionary   = (NSDictionary*)responseObject;
            NSMutableDictionary *responseData = [NSMutableDictionary new];
            if ([dictionary objectForKey:@"responseData"]) {
                responseData = [[dictionary objectForKey:@"responseData"] mutableCopy];
            }
            [responseData setValue:entity forKey:@"entityName"];
            [responseData setValue:[NSNumber numberWithInt:timeStamp] forKey:@"lastSync"];
            [[NSNotificationCenter defaultCenter]
             postNotificationName:notification
             object:nil userInfo:responseData];

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];