Is there a way to mark responses as ones that should be sent base64 encoded to be returned via api gateway? #452
-
Using a raw lambda proxy integration you can do something like: return {
statusCode: 200,
body: myBase64EncodedImage,
isBase64Encoded: true
} and you are able to send binary files back via API Gateway (up to 10mb). Does lambda web adapter expose functionality to do the same thing somehow or is this not supported? I didn't see base64 encoding called out anywhere so I don't know if I missed it or not. My current lambda service is able to return html and css just fine but when I try to return an image that is bundled with my container the content of the body seems to get stripped out. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Lambda Web Adapter does support binary contents. LWA uses lambda-http crate to handle that. Lambda-http will detect binary contents based on content-type and content-encoding headers, and auto base64 encode the data. You don't need to encode the binary data yourself. Here are the content-type prefixes and suffixes that are treated as text. Others are treated as binary. |
Beta Was this translation helpful? Give feedback.
-
You can take a look at the Nginx example. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the links to the code, was able to gleam from there that anything with a proper In my express app it looks something like this, if anyone else runs into this issue: import express from 'express'
import compression from 'compression'
...
const shouldCompressResponse = (req: express.Request) => {
// compress images so apigateway can return them w/ binary media types
logger.info('extension: ', path.extname(req.url))
return ['.jpg', '.png', '.ico'].includes(path.extname(req.url))
}
app.use('/ui/assets', compression({ filter: shouldCompressResponse }))
app.use('/ui/assets', express.static('public/assets/')) |
Beta Was this translation helpful? Give feedback.
Lambda Web Adapter does support binary contents. LWA uses lambda-http crate to handle that. Lambda-http will detect binary contents based on content-type and content-encoding headers, and auto base64 encode the data. You don't need to encode the binary data yourself.
Here are the content-type prefixes and suffixes that are treated as text. Others are treated as binary.