Skip to Content
🎉 Bref 3.0 is released. Read more →
DocumentationUse casesHTTP applicationsBinary requests and responses

Binary requests and responses

AWS Lambda is only used for executing code. Serving assets via PHP does not make sense as this would be a waste of resources and money.

Deploying a website with assets (e.g. CSS, JavaScript, images) is covered in the “Websites” documentation.

In some cases however, you want to serve images (or other assets) via PHP. One example would be if you served generated images or PDFs via PHP.

To return binary responses like images, PDF, binary files…, set the BREF_BINARY_RESPONSES environment variable:

serverless.yml
provider: # ... environment: BREF_BINARY_RESPONSES: '1'

Bref will then encode responses to base64, which is what API Gateway expects for binary responses.

If you use API Gateway v1 REST APIs (http events instead of httpApi), also set binaryMediaTypes, otherwise API Gateway does not support binary requests and responses:

serverless.yml
provider: # ... apiGateway: binaryMediaTypes: - '*/*'

Be aware that the max upload and download size is 6MB and because base64 encoding adds a ~33% overhead  to the response, downloads are limited to ~4.5MB in size. For larger files, use AWS S3. An example is available in Serverless Visually Explained .

Last updated on