Encoding Response as Gzip Causing Cookie to Not Be Set in Browser

Viewed 1087

I'm trying to reduce the network traffic by Gzipping all responses from backend, but I ran into an issue where cookie is not set in the browser if the login/logout call is being done the same way and I'm not sure why.

This is the backend code in Java.

Cookie cookie = new Cookie("cookie_name", cookieToken);
cookie.setPath("/");
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);

response.setContentType("application/json; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
                    
response.setHeader("Content-Encoding", "gzip");
String json=serviceOutput.toString();
byte[] gzip = Utils.gzip(json.getBytes("UTF-8"));
response.setContentLength(gzip.length);
response.getOutputStream().write(gzip);

Any pointer on why this doesn't work? I'm pretty sure I did the same thing previously without any issues.

--

Added the gzip method just in case it's relevant

public static byte[] gzip(byte[] content) throws IOException    {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream g = new GZIPOutputStream(baos);

    if (content != null && content.length > 0) {
        g.write(content, 0, content.length);
        g.close();
    }

    return baos.toByteArray();
}
2 Answers
Related