Sergey
Sergey

Reputation: 1208

AppEngine gzip compressing

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?

Upvotes: 8

Views: 3421

Answers (2)

matt burns
matt burns

Reputation: 25370

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

Upvotes: 0

Nick Johnson
Nick Johnson

Reputation: 101149

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.

Upvotes: 10

Related Questions