This question is extension to the question here. I am using the code here reproduced below to GZIP compress a JSONObject
.
String foo = "value";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(foo.getBytes("UTF-8"));
} finally {
if (gzos != null) try { gzos.close(); } catch (IOException ignore) {};
}
byte[] fooGzippedBytes = baos.toByteArray();
I am using a DefaultHttpClient
to send this compressed JSONObject to server(the code is in my control).
My Question
What header should I use in my request
? I am using request.setHeader("Content-type", "application/json");
for sending JSON to server?
"gzip" is often also used to refer to the gzip file format, which is: a 10-byte header, containing a magic number ( 1f 8b ), the compression method ( 08 for DEFLATE), 1-byte of header flags, a 4-byte timestamp, compression flags and the operating system ID.
Check the HTTP Header in Developer Tools. To check for GZIP compression without an external tool, you can use the developer tools panel in your browser. Most browsers let you inspect page elements and view performance information this way. First, load the web page you want to check in your browser.
In HTTP/1.1 headers in every request are sent uncompressed on the network. However, schemes such as gzip or Deflate are used for content encoding.
Since JSON is text-based, it can be compressed using Gzip or Deflate compression to reduce the payload even further.
To inform the server that you are sending gzip-encoded data, send the Content-Encoding header, not Accept-Encoding.
This answer shows you that you need to set a header indicating that you are sending data compressed:
HttpUriRequest request = new HttpGet(url);
request.addHeader("Content-Encoding", "gzip");
// ...
httpClient.execute(request);
The answer also shows how to deal with the incoming compressed data.
Content-Type:application/json
- tells what type of content data in the http callContent-Encoding: gzip
- tells what compression being used.application/json
or text/xml
or other type can be compressed as gzip and sending to receiver and with Content-Type
header only, receiver will identify the incoming data is of type json/xml/text
and then convert back to object of type json/xml/text.
With Content-Encoding
header only receiver will identify the incoming data is getting gzip compressed. Then receiver required to decompress the incoming data and use it.
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