When running Yii2 on Lambda through Bref, I get an error when returning JSON. The major issue that seems to cause this error is the fact that Yii sets a charset on the response. This causes the Bref runtime to base64 encode the response.
See this issue on Bref https://github.com/brefphp/bref/issues/482
I can manually set the response, without using $this->asJson($data) and it returns as expected. I also manually set the response formatter in the config (using micro framework setup) but it sill not override the contentType.
<?php
return [
'id' => 'micro-app',
// the basePath of the application will be the `micro-app` directory
'basePath' => __DIR__,
// this is where the application will find all controllers
'controllerNamespace' => 'micro\controllers',
// set an alias to enable autoloading of classes from the 'micro' namespace
'aliases' => [
'@micro' => __DIR__,
],
'components' => [
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
'response' => [
'formatters' => [
\yii\web\Response::FORMAT_JSON => [
'class' => 'yii\web\JsonResponseFormatter',
'contentType' => 'application/json',
'prettyPrint' => YII_DEBUG,
'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
],
],
],
]
];
The Lambda function should return a JSON response.
Base64 encoded response, not JSON.
| Q | A
| ---------------- | ---
| Yii version | 2.0.29
| PHP version | 7.3.9
| Operating system | macOS/AWS Lambda/Bref
First of all, it's really interesting that Yii is used in such unusual environment. If you'd decide to put together a tutorial, you'd likely to be first or at least one of the first.
The issue looks to be on the bref side and I think it is better to be fixed there since specifying encoding among with content type is correct. I'm reading bref code for the first time but it should be https://github.com/brefphp/bref/blob/5ee5f6382bfbb9797d2c22565243150a34d666c8/src/Runtime/LambdaRuntime.php#L254. @mnapoli, correct?
Anyway, I believe that it will be fixed in bref soon so closing the issue here. You can speed up things by pull requesting to bref adjusting check to use strpos.
:+1: indeed the issue is in Bref, we should fix that asap!
@jasonmccallister feel free to send a pull request in Bref.
Will do @mnapoli!
Most helpful comment
:+1: indeed the issue is in Bref, we should fix that asap!
@jasonmccallister feel free to send a pull request in Bref.