I'm developing iPhone app and manually constructing POST requests. Currently, need to compress JSON data before sending it, so looking how to tell a server the content is compressed. Setting content type header to gzip might be not acceptable because server expects JSON data. I'm looking for transparent solution, something like just to add some header telling JSON data is compressed into gzip.
I know, the standard way is to tell the server that the client accepts encoding, but you need to make GET request with accept encoding header first. In my case, I want to post the data already encoded.
To compress the HTTP request body, you must attach the HTTP header indicating the sending of HTTP request body compressed in gzip format while sending the request message from the Web Service client. Implement the processing for attaching the HTTP header in the client application.
The “content-encoding: gzip” HTTP Response Header You can open up Chrome DevTools or Firefox Developer Tools and look for this response header under the Network section.
Add a method called DeflateCompression. In the preceding class, a value is passed using headers in a variable called contentencoding. It contains the value as GZip/Deflate/No. If No is passed, then the compression method will not be applicable and plain JSON data will be returned.
Gzip is a file format and software application used on Unix and Unix-like systems to compress HTTP content before it's served to a client.
Include a Obj-C gzip wrapper, for example NSData+GZip
, and use it to encode the body of your NSURLRequest
. Also remember to set the Content-Encoding
accordingly, so the webserver will know how to treat your request.
NSData *requestBodyData = [yourData gzippedData];
NSString *postLength = [NSString stringWithFormat:@"%d", requestBodyData.length];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
[request setHTTPBody:requestBodyData];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With