Gzip compression in AppEngine

I am trying to gzip responses from the GAE server, but getting 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 read that on GAE servers automatic compression. So what could be the problem?

+7
source share
2 answers

Engine external application servers rely on a number of factors, including Accept-Encoding and User-Agent headers, to determine if they should compress responses. They do this because there are several user agents who claim to accept gzipped responses, but actually cannot understand them.

Try setting your user agent to something reasonable (and not "gzip", which is not a real user agent), and see if it matters.

+9
source

In my case, the problem was that the servlet did not specify a value for the Content-Type header. I should have indicated this explicitly:

 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { ... resp.setHeader("Content-Type", "application/json"); ... 
0
source

Source: https://habr.com/ru/post/900660/


All Articles