Search code examples
phpiosjsongzipafnetworking-2

how to use gzip decompression with AFNetworking API call?


I am using AFNetworking library in my projects. recently I heard about gzip data compression which is given by defalut in NSURLConnection Class and reducing the time and loading time of large json response, hence AFNetworking might have that feature as it is working on top of NSURLConnection.

but I do not know how to get gzip compressed json response from php API through AFNetworking.

I need this technique when Json response file size is more that 100kb+.


Solution

  • If server supports gzip it could require client to ask server to respond with gzip enabled. To ask server to use gzip you add specific "Accept-Encoding" header to your requests. You can do this, for example, with this lines of code:

    // Get one that serializes your requests, 
    // for example from your AFHTTPSessionManager subclass
    AFHTTPRequestSerializer <AFURLRequestSerialization> *requestSerializer = ...
    [requestSerializer setValue:@"gzip, identity" forHTTPHeaderField:@"Accept-Encoding"];
    

    "Accept-Encoding" header must contain gzip while, probably, identity is not required.