title: Amazon Lambda Java function returning png to API gateway
body: <p>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:</p>
  
  <pre><code>public static void generatePng(InputStream request, OutputStream response, Context context) 
  </code></pre>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>In the Lambda Integration Response I have tried adding header mappings of:</p>
  
  <p>Content-Length  integration.response.header.Content-Length<br>
  Content-Type    integration.response.header.Content-Type</p>
  
  <p>and a body mapping of:
  content-type: image/png with a template of $input.body</p>
  
  <p>I have also added a response model of image/png in the Method Response section. </p>
  
  <p>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:</p>
  
  <p>e.g.: Response Body</p>
  
  <pre><code> ?PNG
  
  
   IHDR?
   ??~LIDATx? ...     
  </code></pre>
  
  <p>The response headers do show:
      {"Content-Type":"image/png"}</p>
  
  <p>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?</p>