title: How do I send a binary response with AWS Lambda with API Gateway in Node.js?
body: <p>I'm trying to set up a Lambda and API Gateway that will do a s3.getObject() and output the binary image as a response. Eventually I'd like to pull an image from s3 and resize on the fly instead of saving them back to s3, however I can't seem to get even a simple image to output.</p>
  
  <p>My simple lambda looks like this:</p>
  
  <pre><code>'use strict';
  
  const http = require('http');    
  
  exports.handler = (event, context, callback) =&gt; {
      http.get('http://i.stack.imgur.com/PIFN0.jpg', function(res) {
          var body = '';
          res.on('data', function(chunk) {
              // Agregates chunks
              body += chunk;
          });
          res.on('end', function() {
              callback(null, body);
          });
      });    
  };
  </code></pre>
  
  <p>I've set the API Gateway Binary Support to allow 'image/jpeg' and I've tried setting the Content Type in the Method Response and Integration Response. </p>
  
  <p><strong>Method Response:</strong>
  <a href="https://i.stack.imgur.com/r4siQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/r4siQ.png" alt="enter image description here"></a></p>
  
  <p><strong>Integration Response:</strong>
  <a href="https://i.stack.imgur.com/n70wg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/n70wg.png" alt="enter image description here"></a></p>