roof
roof

Reputation: 450

AFNetworking 2.0 always return 403

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.

Upvotes: 0

Views: 818

Answers (1)

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

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) {

}];

Upvotes: 2

Related Questions