Sending binary data (image)

Hello, I’m using aws/lambda with serverless and have an endpoint I’d like to render a gif. I’ve added ‘image/gif’ to Binary Support in API Gateway and have the following in my lambda handler

const response = {
    statusCode: 200,
    headers: {
      'Content-Type': 'image/gif'
    },
    body: new Buffer("data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7", 'binary').toString('base64'),
    isBase64Encoded: true
  }

This shows the correct content-type, but it doesn’t actually render the image. I’ve seen some posts here, that it’s not supported, then some that it is supported and there’s plugins. Seems like things are partially working for me here, but perhaps a setting in API gateway? Any suggestions would be great.

Apparently using this hex seems to work or at least render the image (1px)

 var imageHex = "\x42\x4d\x3c\x00\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00"+ 
  "\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x18\x00\x00\x00"+ 
  "\x00\x00\x06\x00\x00\x00\x27\x00\x00\x00\x27\x00\x00\x00\x00\x00"+
  "\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00"; 

I found this here: https://chrislarson.me/blog/tutorial/2016/12/16/1x1_pixel_aws_lambda.html

Not quite sure why this would work but my base64 wouldn’t. I tried multiple variations of the base64 (excluding data:image/gif;base64, and only sending the base64 string, but none of that seemed to render in the browser.

I don’t have an exact answer but struggled with it myself, so some pointer:

  • Pls make sure it is “THAT” 1px that exact 1 px shown. To me, gateway shown 1px picture instead the picture from s3 which I wanted to show
  • YOLO check: don’t forget to “deploy” your API Gateway changes after you added the context type.

Once you done, consider using a plugin to keep your APIGateway configuration as part of serverless code.


or https://github.com/ryanmurakami/serverless-apigwy-binary

Ask someone else why the hell there are two and how are they different :troll: