2

I have a requirement to return a PNG from an Amazon Lambda Java Function to a Amazon Rest API. I have built the function with a function prototype like this:

public static void generatePng(InputStream request, OutputStream response, Context context) 

The InputStream request details the information that needs to be written into the image. The OutputStream response is what is sent back and is a png image. I have tested the code and know that the image encoded in to the output stream is correct as my test writes this to a file which I can see in an image editor.

The issue I have is in the wiring of the response from the Lambda Function to the API gateway. The documents talk exclusively of a String response but my response payload is binary image data.

In the Lambda Integration Response I have tried adding header mappings of:

Content-Length integration.response.header.Content-Length
Content-Type integration.response.header.Content-Type

and a body mapping of: content-type: image/png with a template of $input.body

I have also added a response model of image/png in the Method Response section.

On testing the API, the response to the API call does not show the image though, but rather a string dump of the image starting with the proper png header:

e.g.: Response Body

 ?PNG


 IHDR?
 ??~LIDATx? ...     

The response headers do show: {"Content-Type":"image/png"}

Does anybody know how to configure Amazon API gateway to accept an image as the output of a lambda function call and render it back to the caller properly?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
theINtoy
  • 3,388
  • 2
  • 37
  • 60
  • 2
    API Gateway doesn't currently support binary data: https://forums.aws.amazon.com/thread.jspa?threadID=195218 – Mark B May 17 '16 at 19:33
  • In the end I Base64 encoded the image and replied with a JSon payload of the PNG as a base64 encoded image. – theINtoy Jun 24 '16 at 14:12
  • What you could also do is upload the generated PNG to S3 and have your lambda return a 302 redirect to the image via API Gateway. Solved a similar problem like that. – Theodor Oct 11 '16 at 06:27

1 Answers1

1

API Gateway doesn't support binary data - https://forums.aws.amazon.com/search.jspa?objID=f199&q=binary&x=0&y=0.

You could consider using the base64 encode/decode feature in the mapping template. Link

Abhigna Nagaraja
  • 1,874
  • 15
  • 17
  • API Gateway has recently added binary data support, haven't found much information online on getting it to work. – DBrown2207 Jan 10 '17 at 03:54