-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for serving binary responses #312
Comments
This was merged recently in #250 - should go out with the next release. |
Haha ohwell, that is convenient - will have a look and reopen this if needed. |
I've had a look through the changes and the tests and tried to come up with a configuration for it: sam version: 0.27
The response object in my lambda looks like this:
Using this configuration in the sam.yaml file, sam local starts and an invocation to /transform delivers the transformed response - however as a base64 encoded string. I was expecting it to be binary data, since I've configured x-amazon-apigateway-binary-media-types. |
Hey @ferencbeutel, that is indeed the expected behavior. Lambda can only receive a JSON payload, it does not understand binary data. For this reason, when you configure binary content types in API Gateway, the request payload is base64 encoded by API Gateway and passed on to Lambda as a base64 string in the body field. If you use frameworks such as serverless-java-container or serverless-express, the data is decoded automatically and passed to your code as if it was the original binary payload. If you don't use any frameworks, you will have to handle this use-case yourself. |
hey @sapessi, sorry for making it unclear that I was talking about the response, not the request payload. When I configure my api gateway in my amazon aws account, I can set the BinaryMediaTypes option, which automatically decodes the base64 response my lambda function returns into binary data. I was expecting to replicate that api gateway behaviour with sam local somehow. |
this should also work with SAM local. You should only have to set the Does that not work? |
Hey @sapessi, I am afraid that I cannot get it to return binary data. For further investigation, I've provided a simple testing setup. EDIT: My bad, I wasnt aware of the fact that one has to match the Content-Type exactly with the Accept header. Doing so provides me with the decoded response. Thanks for your patience :) |
Sorry for reopening this once again, but the implemented logic in sam local sadly does not match amazon api gateways logic. On the api gateway, It is possible to configure the media types, which should be encoded/decoded by the gateway, in the BinaryMediaTypes field. If the Accept-Header of the request or the content-type header of the response matches one of the values configured in the binaryMediaTypes option of the api gateway, the encoding/decoding is done. I've got this information from here. |
Thanks for filing this issue, I am having the exact same problem where I can not use SAM local to develop my image manipulation lambda. I can |
To get around this in local dev and allow in browser view of the images is to create a base64 CSS'ed representation that mimics what the browser would do for a raw image load in a window. Write your own condition code that calls this. const imgData = `data:image/jpg;base64,${data}`;
return `<body style="
margin: 0;
width: 100vw;
height: 100vh;
background-image: url(${imgData});
background-size: contain;
background-repeat: no-repeat;">
</body>` |
Likewise, for local dev I return text/html headers. However, I would very very much like local dev to be what I see when this is deployed so I can QA the header requirements or use the lambda locally as a |
SAM CLI latest version (v0.3.0) now supports the same functionality that APIGW supports for binary media types. Closing this issue. Feel free to re-open if it doesn't work in latest version. |
Thanks for the note, but I could not confirm this because #457 blocked my upgrade. |
Hi @sanathkr thats great to hear! I couldnt find any documentation about this, could you elaborate a little bit about how to configure the BinaryMediaTypes value? |
I can confirm this works. Thanks y'all. |
For anyone ending up here trying to make it work, and wondering exactly what the solution was. By adding this to my template.yaml
I was able to return a gif image without having to set the Accept header in the request. And it seems that adding |
Worked just fine for me, but mine serves any binary data format. |
Hello, I came across a strange issue when trying to add Binary Data Types to my template.yaml file. This issue is also mentioned in 'Images not served properly' here : I am setting the binary data type as follows in the SAM template.yaml: MyApi: I have a lambda function which returns a jpeg image as base64 in the lambda handler as follows: ` obj = s3.Object(bucket_name, item_name) return { When testing the api locally using sam local strart-api, it works perfectly regardless of the request Access header value. But when I deploy the API, I can only get the correct behaviour if the request Access header is set to 'image/*'. If It is not set then the base64 is returned instead. Why is this and is there any workaround? |
This totally does not work, whatever you do. |
It works with ' |
Hello there,
currently I am trying to serve an image from a lambda function as a binary response.
That is working very well as I specified
BinaryMediaTypes:
- "*/*"
On my RestApi Cloudformation template.
However, now I am trying to test that api locally with the help of sam local and found no way to define the aformentioned parameter in the sam.yaml file.
Can you assist with the correct setup for serving binary responses with sam local?
Thanks
The text was updated successfully, but these errors were encountered: