0.5.7
-
Fix #372 with #466 Allow the region to be a variable in
serverless.yml
, by @harmenjanssen -
#288, #457 Support binary responses (images, PDF, etc.) by @victormacko, @shrink, @Guillaume-Rossignol, @italo1983 and @bubba-h57.
It took months but we finally have a solution that requires the least effort from users, and supports the most use cases. Bref will automatically detect if the response is a text response or a binary response, and will behave accordingly (binary responses need to be sent as base64-encoded to API Gateway).
Below is an extract of the new documentation that can be read at https://bref.sh/docs/runtimes/http.html#binary-responses:
By default API Gateway does not support binary HTTP responses like images, PDF, binary files… To achieve this, you need to enable the option for binary responses in serverless.yml
:
provider:
# ...
apiGateway:
binaryMediaTypes:
- '*/*'
This will make API Gateway support binary responses for all responses. Your application can now return binary responses as usual.
However, you must define a Content-Type
header on binary responses. If you don't, you may get the following error: Failed encoding Lambda JSON response: Malformed UTF-8 characters, possibly incorrectly encoded. Symfony's helpers or Laravel's helpers will take care of that for you. If you don't use them, here are some examples:
// Vanilla PHP example with a JPEG image response:
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($filename));
fpassthru(fopen($filename, 'rb'));
// PSR-7 example:
return $response
->withHeader('Content-Type', 'image/jpeg')
->withHeader('Content-Length', (string) filesize($filename))
->withBody(new Stream($filename));