How to add binary media type to serverless.yml

Hello. I have a question.

I want to send to Lambda with binary data encoded in serverless.yml.
It can be set in APIGateway, but it can not be set in serverless.yml.
Could you tell me what kind of propaty should be used?

1 Like

Hi yamazaki,
I think there is still no “proper” way to do this in serverless but there’s some plugin.
See https://github.com/serverless/serverless/issues/2797
for details.

1 Like

Thank you bbilget.

I have tried increasingly what is in this article.
We inserted ContentHandling: CONVERT_TO_BINARY and set MediaType from APIGateway.
However, the execution result is ‘internal server error’.
If you specify Media Type on the API Gateway, the above error will be returned.
How can I base64encod binary data with serverless and payload it to Lambda?

Hi yamazaki,
Handling binary data with APIGW is a little tricky.
You can check my example for Java/JRestless here: https://github.com/bbilger/jrestless-examples/blob/master/aws/gateway/aws-gateway-binary/README.md

Aside from this:

  • you must encode your data with base64 yourself when returning binary data (and set the “isBase64Encoded” attribute to true on the proxy response object)
  • you must decode binary data with base64 when receiving binary data (the proxy request object will have the attribute “isBase64Encoded” set to true)
  • APIGW will handle binary data (request/response) only when the request has proper Content-Type / Accept headers set with one of the ones you registered as binary media types

Thank you bbilger.

I hava a once question.
How to set isBase64Encoded with APIGateway?
Method Request or Integration request?

You don’t. You have to set it on the proxy response object in your lambda function - assuming you are using proxy integration.
See: https://forums.aws.amazon.com/thread.jspa?messageID=753476&#753476
if you post/put binary data, APIGW will set the flag on the request object.

Hello,

Check out serverless plugin, written by me :slight_smile:

There is also examples provided, one with angular, and one with express.

PS: Stars and contribution are welcome!

5 Likes

Thank you bbilger and maciejtreder.

This problem was solved.
I hope serverless will be easy to use.

1 Like

maciejtreder, sir, you are a life saver. Thank you. Though I will say, using this config instead in my serverless.yml seem to work better for my png case:

apigwBinary:
types:
- ‘/’

Is this still necessary with serverless v2.2.0? I’m really struggling to work with the data that is received in a lambda_proxy function when it’s an image.

1 Like