Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTTP POST request with gziped content?

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.

like image 995
Centurion Avatar asked May 02 '12 09:05

Centurion


People also ask

How do I send a compressed HTTP request?

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.

Where do I put content encoding gzip?

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.

How do I use gzip compression in web API?

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.

What is content encoding gzip?

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.


1 Answers

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];
like image 110
Andreas Ley Avatar answered Oct 16 '22 22:10

Andreas Ley