Ngchiwa Ng
1 min readJul 25, 2018

AWS api gateway return binary image

in this article, just setup to response binary image via lambda :

  • lambda response: {headers: { ‘Content-Type’: ‘image/png’}, isBase64Encoded: true}
  • API Gateway: Setting Binary Media Type */* and lambda response

Lambda

just return base64 image string

exports.handler = async (event) => {
let result = "data:image/png;base64,....".replace(/data:image\/png;base64,/, '');
return {
statusCode: 200,
headers: {
'Content-Type': 'image/png'
},
body: result,
isBase64Encoded: true
}
}

API Gateway

api right Setting add Binary Media Types

:)

ref:

https://medium.com/@adil/how-to-send-an-image-as-a-response-via-aws-lambda-and-api-gateway-3820f3d4b6c8

(not use Binary Media Type) https://sanderknape.com/2017/02/dynamic-image-generation-with-aws-api-gateway-and-lambda/

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html

Ngchiwa Ng
Ngchiwa Ng

Written by Ngchiwa Ng

Backend/iOS Engineer, rock the world

Responses (4)