-
-
Notifications
You must be signed in to change notification settings - Fork 366
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
Add support for binary HTTP responses (using base64 encoding) #288
Comments
Hi, this is very hard to read and understand. Maybe you could open a pull request instead? And please could you explain why this is better, and whether there are downsides to encoding everything with base64? |
The official docs provide the Output Format of a Lambda Function Proxy Integration as:
so the API Gateway expects to be told whether it is encoded or not, whether it is encoded or not. The reasons for using Base64 encoding is typically because the response it binary. From the AWS Documentation:
Ref: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format |
Since we use PHP-FPM it's not really simple to choose whether to use base64 or not from PHP. I guess the question is: should we always return with base64? If so, are there downsides? I'm guessing that the response size might be bigger? |
We should not always return base64. I recommend bref should inspect the headers from PHP-FPM to determine if it should be base64 encoded. For example, If the PHP-FPM header states that is a Binary Media Type we should encode it prior to sending it on to API Gateway. It could also be a zip file, PDF, or some other type, not just images or video. I can think of a number of use cases for dynamically creating zips, pdf's or images and returning them. |
Exactly, in my case i've used the lambda for creating zip files and pdf but also to output barcode and qrcode in differents formats.. Maybe there's a better way to check if the content is base64 but my code is working very well on a production environment. PS. When using base64 encoded output we need to configure API Gateway to accept proper media type or using a regular expression (es. */*) |
Here is how I handle a similar issue in our laravel-bref-bridge package bootstrap. |
@bubba-h57 makes a lot of sense! I'm not sure if it will be easy to build a reliable detection of binary responses. I mean there are a lot of content types: http://www.iana.org/assignments/media-types/media-types.xhtml Another option would be to support a custom |
Here is an idea: we need to encode in base64 if the response cannot be JSON encoded. So why not try to always That way users don't have anything to do. |
That could negatively impact response time. Reading headers should be better, I believe. |
I think i'm encountering this as well when trying to output image data as a response. The error which comes back is; I've tried to encode the $data var in LambdaRuntime.php:248 using utf8_encode (which is an array, so doesn't encode), and the output JSON (which is 'false' (bool) ... I guess due to not being able to encoded). |
This is finally done! Thanks @victormacko, @shrink, @Guillaume-Rossignol, @italo1983, @bubba-h57 and all that participated. This was one of our most common issues and I'm really happy that we finally have a solution :) I'll tag a release tomorrow! |
Tried with the function validBase64($string) $fileMimeType = Storage::disk('s3')->mimeType($fileLocation); |
Even I tried
then the downloaded file is corrupted. |
@ashish-dirkmedia-de please, plenty of people were involved in this issue that has been closed for some time. You duplicated your messages on several issues, this is not a behavior that is appropriate towards other project contributors and users. Let's cool it off for a day and please open a separate issue with complete details, and let's stick to that new issue. |
Intro added by @mnapoli to clarify the issue following the discussion
API Gateway does not support well returning binary responses (for example PDF files). To support that, we need to return the response as base64 encoded. This is not something that should be done in your application: this is something that we need to implement in Bref.
Please apply this mod to LambdaResponse.php file to enable isBase64Encoded flag automatically:
public function toApiGatewayFormat(): array { // The headers must be a JSON object. If the PHP array is empty it is // serialized to
[](we want
{}`) so we force it to an empty object.$headers = empty($this->headers) ? new \stdClass : $this->headers;
The text was updated successfully, but these errors were encountered: