How to Send an Image as a Response via AWS Lambda and API Gateway?

Sending an image as a response via AWS Lambda and API Gateway is not that straightforward.

I assume that you already have a connection between Lambda and API Gateway and you use LAMBDA_PROXY in the Integration Request.

In this blog post, I’ll walk you through the 3-step process of generating the image files. The example code is:

You must convert your image to a base64 string. So, you will send your image as a base64 string actually. Yeah, that sounds weird.

That’s all of it?

Nope.

You send the image as a base64 string. API Gateway must convert it from base64 to binary. You must add CONVERT_TO_BINARY to contentHandling path.

And… You must add */* to binaryMediaTypes. Go to your API’s settings page:

You can edit the binaryMediaTypes via CLI as well:

If you want to add / to binaryMediaTypes then you must use ~1 instead of / while using CLI.

That’s it. Don’t forget to redeploy your API Gateway.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store