37

Does anyone knows if NSURLConnection/NSURLRequest have support for gzip requests.

If does, can you provide more information?

Donald Duck
  • 6,488
  • 18
  • 59
  • 79
mxg
  • 19,703
  • 12
  • 56
  • 77
  • Possible duplicate of [Accept-Encoding: gzip on iOS](http://stackoverflow.com/questions/25333857/accept-encoding-gzip-on-ios) – jscs Nov 01 '16 at 01:07
  • @JoshCaswell, please check the date difference. It was asked 5 years after this question was posted. – mxg Nov 03 '16 at 15:05
  • [Duplicates don't always have to point in the direction of the later date](http://meta.stackexchange.com/a/147651/159251). I believe the answer at the other question is a bit more useful than those found here. – jscs Nov 03 '16 at 15:59
  • https://stackoverflow.com/questions/41044239/nsurlsession-download-and-do-not-decompress-gzip. `Accept-Encoding: gzip, deflate` is automatically added to the request's header [NSURLSession]. – abhimuralidharan Sep 16 '19 at 05:59

2 Answers2

66

although it does not seem to be documented, there is evidence that NSURLConnection does have transparent gzip support. meaning that if the server supports gzip encoding, and your request has an Accept-Encoding header containing gzip*, the server will send a gzipped response, which NSURLConnection will automatically decode.

* NSURLRequest might add that header by default. if not, you have to add it manually like so:

 [urlReq setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]
jscs
  • 62,161
  • 12
  • 145
  • 186
ax.
  • 53,672
  • 7
  • 72
  • 66
  • 8
    1. i think you need an NSMutableURLRequest to add a header 2. as of "iOS 3.2" as base sdk, the accept-encoding header containing deflate and gzip seems to be included automatically (apparently didn't used to be). –  Dec 08 '10 at 15:38
  • 6
    I've just done tests as well and saw that iPhone adds gzip and deflate by default. Does anyone know if it was always supported or since which version Apple has started including Accept-Encoding by default? – Dmitry Feb 11 '11 at 18:17
  • 3
    What about gzip encoding requests sent to the server? http://stackoverflow.com/questions/6204066/how-to-gzip-an-nsurlconnection-request – ma11hew28 Jun 01 '11 at 15:56
  • 1
    After receiving the data, do I have to unzip the received data? – Satyam Feb 07 '13 at 12:51
  • 4
    @Satyamsvv _[...]... the server will send a gzipped response, which NSURLConnection **will automatically decode**._ – ax. Feb 07 '13 at 13:19
  • 1
    How can I verify if this is taking affect? – Jared Egan Sep 30 '14 at 01:51
  • How to get Response Data before compression? What layer does this decompression happen ?CFNetwork? – Beyond Chao Jan 05 '18 at 02:08
  • @BeyondChao Do you mean "response data before **_de_**compression"? – ax. Jan 05 '18 at 09:46
3

NSURLRequest decodes gzip to NSData; such as the Server response contain "Content-Encoding" = gzip; the NSData will decode. If you don't want to automatically decode it, add the code below. This is a private API.

//import CFNetwork.framework
extern CFStringRef kCFURLRequestDoNotDecodeData;
typedef const struct _CFURLRequest* CFURLRequestRef;
extern void _CFURLRequestSetProtocolProperty(CFURLRequestRef,CFStringRef,CFTypeRef);

//NSURLRequest init ...
//...
CFURLRequestRef requestRef = (CFURLRequestRef)[request performSelector:@selector(_CFURLRequest)];
_CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);
shim
  • 7,170
  • 10
  • 62
  • 95
liuyuning
  • 141
  • 1
  • 5
  • I filed a request at bugreport.apple.com that this or an equivalent API be made public. http://openradar.appspot.com/radar?id=4931101243998208 – John Mar 15 '16 at 23:38
  • Did this ever get solved? I need a way to download a stream without it being automatically unzipped. – casolorz Aug 20 '18 at 23:26
  • @casolorz This bug has fixed on recent iOS version,it work well on iOS8 and later – liuyuning Sep 09 '18 at 09:57
  • @casolorz can you elaborate? the "bug" is here described as a missing API - so what API fills the gap? how do you control an NSURLSession, or the request/response to use gzip (or not)? how do you instruct the NSURLSession to leave the response encoded, and hand it to you as is? – Motti Shneor May 29 '19 at 11:39
  • @MottiShneor think you meant to ask someone else that? – casolorz May 29 '19 at 13:28