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

Support for REST APIs that return binary data such as pdfs and images #121

Open
2 tasks
hisham opened this issue Jan 29, 2022 · 1 comment
Open
2 tasks
Labels
api-rest feature-request New feature or request

Comments

@hisham
Copy link

hisham commented Jan 29, 2022

Is this feature request related to a new or existing Amplify category?

api

Is this related to another service?

API Gateway

Describe the feature you'd like to request

It seems like having a REST API configured through API Gateway to return binary data is not as easy as one would think.

From what I gathered this is what needs to be done:

  1. Add the binaryMediaType (e.g. application/pdf) in the API Gateway settings via override.ts (e.g. resources.restApi.binaryMediaTypes = ['application/pdf'];)
  2. Add this exact same binary media type to the aws-serverless-express server creation call - (i.e. const server = awsServerlessExpress.createServer( app, null, // binary mime types - this will cause the lambda to base64 encode the response body and return isBase64Encoded=true ['application/pdf'] );). Apparently this is not needed with the latest vendia package.
  3. Make sure the client requesting the binary data has the first entry in its accept header the same as what's specified in binaryMediaTypes. This is ofcourse impossible to control in a straight up browser link. And setting binaryMediaTypes to */* in API Gateway is not a solution as that messes up JSON Rest APIs
  4. Return the binary data and set the content type in the REST API endpoint (res.contentType('application/pdf'); res.send(pdfBuffer))

Describe the solution you'd like

This should at least be documented by amplify cli and perhaps an option to just have amplify cli configure this setting for us, as you need to become an api gateway expert to make this setting work, and even with that there's limitations (if you can't control accept header of client you are out of luck).

Describe alternatives you've considered

Upload the binary data to s3 and do a redirect to a presigned url.

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change
@cjihrig cjihrig added api-rest feature-request New feature or request labels Feb 2, 2022
@alharris-at alharris-at transferred this issue from aws-amplify/amplify-cli May 17, 2022
@JefStat
Copy link

JefStat commented Nov 17, 2022

This is done with the override feature

https://docs.amplify.aws/cli/restapi/override/

and add the following line into the override.ts file
resources.restApi.binaryMediaTypes = ["application~1octet-stream", "application~1zip"];

here's the doc saying use ~1 instead of /
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-binarymediatypes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-rest feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants