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?