1

I am trying to zip the lambda response from java code but in browser it is failing to decode

    @Override
    public Map handleRequest(Map<String,Object> input, Context context) {

           Map<String, Object> repsonse = new HashMap<>();
           HashMap<String, String> headers = new HashMap<>();              
           headers.put("Content-Encoding", "gzip");
           headers.put("Content-Type", "text/html");
           final String sampleHtml = "<h1>Hello World</h1>";  
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           GZIPOutputStream gzip = new GZIPOutputStream(out);
           gzip.write(sampleHtml.getBytes());
           gzip.close();
           String responseString = out.toString("ISO-8859-1");
           response.put("headers", headers);           
           response.put("body", responseString);
           response.put("statusCode", 200);
           response.put("isBase64Encoded", true);
           return response;
    }
Jinu P C
  • 3,146
  • 1
  • 20
  • 29
  • Does `out.toString("ISO-8859-1");` generate base64 encoding? – Michael - sqlbot Nov 24 '17 at 02:07
  • how are you invoking this function? are you using API Gateway? if you are using API Gateway, you don't event need to manually GZip the content as it uses CloudFront under the hood and you can set it to deliver GZip content http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html – Ardavan Kalhori Nov 29 '17 at 01:28
  • 1
    See [this answer](https://stackoverflow.com/a/48758821/39396) for using API Gateway's compression feature. – Carl G Mar 16 '18 at 21:08
  • Jinu, Did you ever solve this issue? I am running into the same issue using the java aws stream handler - it does not seem to be compressing the response and we have hit the limit with the AWS Lambda reponse size (6mb)... It would be really helpful if you solved this and can post your answer! – Jeff May 29 '19 at 15:42
  • @Jeff Please go through this link https://stackoverflow.com/a/48758821/5872782 – Jinu P C May 30 '19 at 06:19
  • @JinuPC thanks mate - alas we're using an ALB to forward our HTTP requests to the lambda not API Gateway. I believe our problem is still Lambda related. – Jeff May 30 '19 at 09:45

0 Answers0