0

I'm trying to create an AWS Lambda accessed by API Gateway which returns gzipped json response.

This is my lambda code (typescript):

import { DB } from '@/DB/db';
import zlib from 'zlib';
import { promisify } from 'util';

export async function main(db: DB) {
    const gzip = promisify(zlib.gzip);

    console.info('procees of getTools tools started');
    const result = await db.getToolsRepo().getTools();

    const jsonString = JSON.stringify(result);

    // Compress with gzip
    const gzipCompressed = await gzip(Buffer.from(jsonString, 'utf-8'));

    // Prepare the response
    const response = {
        statusCode: 200,
        headers: {
            'Content-Type': 'application/json',
            'Content-Encoding': 'gzip'
        },
        body: gzipCompressed.toString('base64'),
        isBase64Encoded: true
    };

    return response;
}

How should I configure API Gateway ? I triend "content handling" - passthrough and "convert to binary" - both does not help - I get nested response instead of base64 binary data:

 {"statusCode":200,"headers":{"Content-Type":"application/json","Content-Encoding":"gzip"},"body":"H4sIAA ...
4
  • hope you got your answer here stackoverflow.com/questions/32464334/… Commented Aug 22, 2024 at 12:42
  • Last answer 7 years ago and does not help Commented Aug 22, 2024 at 12:54
  • API Gateway will compress the response for you, instead of having to do all that in the Lambda, if you follow this guide: docs.aws.amazon.com/apigateway/latest/developerguide/… Commented Aug 22, 2024 at 14:43
  • @PavelBernshtam i was just trying to show people in community that many things change over the time and right answers then wont work now. I did answer a quetion last time and people downvoted and deleted as duplicate whie OP accepted my answer as he/she could not find the right answer in the suggestion. so this first comment was for the moderators hope i think i can answer this now as i dont see people saying duplicated , :P Commented Aug 22, 2024 at 15:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.