Skip to content
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

Binary output/response? #69

Closed
repi opened this issue Jan 10, 2019 · 8 comments
Closed

Binary output/response? #69

repi opened this issue Jan 10, 2019 · 8 comments
Milestone

Comments

@repi
Copy link

repi commented Jan 10, 2019

When using lambda_runtime it seems to be hardcoded that one's lambda handler will take JSON as input and output in JSON and it serialises the structures using serde_json. Is it possible to configure this and esp. be able to return binary data with a specified mime type instead of "application/json"?

This would be most useful for lambdas for example responding with image/png, image/jpg and other binary types.

I also tested using lambda-runtime-client directly as then I have control over the deserialising of the request and are able to directly output a binary blob as the response. But the header for the response is fixed to use const API_CONTENT_TYPE: &str = "application/json";.

Have there been any thoughts of how to support and handle binary responses with other mime types?

@sapessi
Copy link
Contributor

sapessi commented Jan 10, 2019

Hey @repi - the Lambda runtime API only supports JSON, so one way or another we'll have to translate to JSON. For example, API Gateway handles binary data for lambda by base64 encoding it on the way in and expecting a base64 encoded blob out from Lambda - API Gateway takes care of decoding it before shipping it back as a binary response.

Having said all this, we are working on a refactor of the runtime (#63) that exposes a new crate called lambda-runtime-core. The core runtime implements the "main loop" and expects a handler interface that receives a Vec<u8> and returns a Result<Vec<u8>, StdError + Display + LambdaErrorExt>. Hopefully this will be merged and go out over the next few days.

@repi
Copy link
Author

repi commented Jan 10, 2019

Ok thanks. If one have has a Vec<u8> response/output from a lambda_runtime handler, do you know if there are some attributes one can use on that field to make it base64 encoded?

Because today it looks like it just turned into a json text giant number array which is of course very inefficient for a large binary :)

@repi
Copy link
Author

repi commented Jan 13, 2019

For now I manually specify that the Vec<u8> fields in my input & outputs for the lambda should be base64 encoded/decoded using same technique as described here: serde-rs/json#360

@davidbarsky
Copy link
Contributor

@repi:

Hi Johan! Sorry about the delay.

For now I manually specify that the Vec fields in my input & outputs for the lambda should be base64 encoded/decoded using same technique as described here: serde-rs/json#360

I think that's a reasonable workaround for now!

@repi
Copy link
Author

repi commented Jan 14, 2019

Thanks!

@softprops
Copy link
Contributor

@repi it's unreleased but the lambda-http module handles this automatically for you. I can it's not easy to find because there hasn't been a release published yet but take a look at the body response formats supported https://github.com/awslabs/aws-lambda-rust-runtime/blob/master/lambda-http/src/body.rs. I'll put together a example soon

@repi
Copy link
Author

repi commented Jan 20, 2019

@softprops ah that looks good, thanks!

@sapessi sapessi added this to the 0.2 milestone Jan 25, 2019
@sapessi
Copy link
Contributor

sapessi commented Jan 25, 2019

Resolving since 0.1 of the lambda-http crate is being pushed now.

@sapessi sapessi closed this as completed Jan 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants