Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What header should be used for sending GZIP compressed JSON from Android Client to Server?

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?

like image 575
Gaurav Agarwal Avatar asked Jul 10 '12 13:07

Gaurav Agarwal


People also ask

What is the gzip header?

"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.

How do I enable gzip compression on my server?

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.

Are HTTP headers compressed?

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.

Can I gzip JSON?

Since JSON is text-based, it can be compressed using Gzip or Deflate compression to reduce the payload even further.


3 Answers

To inform the server that you are sending gzip-encoded data, send the Content-Encoding header, not Accept-Encoding.

like image 167
Michael Hampton Avatar answered Oct 10 '22 02:10

Michael Hampton


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.

like image 40
Audrius Avatar answered Oct 10 '22 02:10

Audrius


  • Content-Type:application/json - tells what type of content data in the http call
  • Content-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.

like image 1
Jagadeesan M Avatar answered Oct 10 '22 02:10

Jagadeesan M