How to use gzip decompression with AFNetworking API call?

I use the AFNetworking library in my projects. I recently heard about gzip data compression, which is given by defalut in the NSURLConnection class and reduces the time and load time of a large json response, so AFNetworking can have this function, since it works on top of NSURLConnection.

but i don't know how to get gzip compressed json response from php API via AFNetworking.

I need this technique when the Json response file size is larger than 100kb +.

+4
source share
2 answers

If the server supports gzip, it may require the client to request the server for a response with it turned on gzip. To ask the server to use gzip, you will add a specific "Accept-Encoding" header to your requests. You can do this, for example, using these 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"the header should contain gzip, while perhaps identitynot required.

+4
source

If you use the API based NSURLSessionin AFNetworking, it will be automatically enabled, as stated in here . This way you do not need to do anything.

, , , gzip.

0

All Articles