8

I'm trying to gzip responses from GAE server, but receive null in Content-Encoding.

I have the following code:

connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", 
          "application/json; charset=utf-8"); //"application/json; charset=utf-8"
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("User-Agent", "gzip");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);

//write
//read

System.out.println("Content-Encoding " + connection.getContentEncoding());

I've read that on GAE servers do compressing automatically. So what can be the problem?

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
Sergey
  • 1,168
  • 2
  • 13
  • 28
  • GAE doesn't take any notice of whether you ask it to compress or not. How big is the file in this case? From experience, anything over 1MB is never compressed. – Frodo Baggins Dec 12 '11 at 13:31

2 Answers2

10

The App Engine frontend servers rely on a number of factors, including the Accept-Encoding and User-Agent headers to determine if they should compress responses. They do this because there are a number of user agents out there that claim to accept gzipped responses, but actually can't understand them.

Try setting your user agent to something sensible (and not 'gzip', which isn't a real user agent), and see if that makes any difference.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • 6
    https://developers.google.com/appengine/kb/general#compression states "To force gzipped content to be served, clients may supply 'gzip' as the value of both the Accept-Encoding and User-Agent request headers.". Some people report it working, others not. Basically, nobody seems to know the real answer. – Frodo Baggins Dec 23 '12 at 11:44
  • Modified client to send `Accept-Encoding: gzip`, but App Engine still responded uncompressed. Then appended gzip to UA, `User-Agent: Our Real UA String; gzip` and that (along with `Accept-Encoding: gzip`) finally forced App Engine to respond compressed. – Zach Young Dec 05 '16 at 18:28
  • Can we do this without sending in Request ? like setting these values inside code ? – Sunil Rk Apr 25 '17 at 06:25
0

In my case, the problem was that the servlet wasn't specifying a value for the Content-Type header. Specifying it explicitly was all I needed to do:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    ...
    resp.setHeader("Content-Type", "application/json");
    ...
matt burns
  • 24,742
  • 13
  • 105
  • 107