sosale151
sosale151

Reputation: 380

AFNetworking unable to serialize gzipped response (response object is nil)

I'm using AFNetworking to download a .gzip file, that when uncompressed should return a JSON string. I've made the get request via my browser, the .gzip file is downloaded and when unzipped, the appropriate JSON is retrieved.

I know AFNetworking is built on NSURLConnection, and from what I read NSURLConnection has gzip inflation in built into it. However, I'm unable to uncompress and parse the gzip file returned by the server into JSON. The response object from AFNetworking remains nil. My code is as follows:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"application/x-gzip", nil];


[manager GET:[NSString stringWithFormat:@"%@%@", BASE_URL, GET_CONTENTS_URL] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if(responseObject == nil){
        NSLog(@"Response is still nil");
    }
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

I'm unsure why this does not work.

Note 1: However, if I replace the response serializer to AFHTTPResponse serializer, the response object is not nil. However, it is of class _NSInlineData, which is an undocumented class.

Upvotes: 2

Views: 1719

Answers (2)

Mingming
Mingming

Reputation: 2219

In my case, I use AFHTTPSessionManager and it worked fine without using AFgzipRequestSerializer to decode the response.

Upvotes: 0

Mangesh
Mangesh

Reputation: 2285

I had the same problem, Finally I get work with the help of AFgzipRequestSerializer.

You need to use "AFgzipRequestSerializer".

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFgzipRequestSerializer  serializerWithSerializer:[AFJSONRequestSerializer serializer]];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

Upvotes: 2

Related Questions