Support New AWS APIGW Binary Responses
Problem
This is a Feature Proposal Description Previously, AWS API Gateway did not support binary responses, making it impossible to return images from your serverless API. Now they do (see https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/). We need to be able to configure HTTP endpoints/events in serverless to use this new functionality.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Enable Binary Responses in AWS API Gateway
AWS API Gateway previously did not support binary responses, limiting the ability to return non-text data such as images. With the introduction of binary support, configurations need to be updated to leverage this new functionality for serverless APIs.
Awaiting Verification
Be the first to verify this fix
- 1
Update API Gateway Settings
Modify the API Gateway settings to enable binary media types. This allows the API to handle binary data such as images.
bashaws apigateway update-rest-api --rest-api-id <api-id> --patch-operations op='add',path='/binaryMediaTypes/*',value='image/png' - 2
Configure Binary Media Types in Serverless Framework
In your serverless.yml file, specify the binary media types that your API will support. This is essential for returning binary data correctly.
yamlbinaryMediaTypes: - image/png - image/jpeg - 3
Update Lambda Function to Return Binary Data
Ensure that your Lambda function returns the appropriate headers and body for binary responses. Set the 'isBase64Encoded' flag to true for binary data.
javascriptreturn { statusCode: 200, headers: { 'Content-Type': 'image/png' }, body: base64ImageData, isBase64Encoded: true }; - 4
Deploy Changes
Deploy your changes to AWS using the Serverless Framework to ensure that the new configurations take effect.
bashsls deploy
Validation
To confirm the fix worked, invoke the API endpoint that returns binary data and check if the response is correctly formatted as an image. Use tools like Postman or curl to verify that the Content-Type header matches the expected type and that the image renders correctly.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep