Bref: Bref version 0.5.7 encoding the HTTP response (JSON) to Base64 string.

Created on 22 Oct 2019  路  14Comments  路  Source: brefphp/bref

Hey @mnapoli , I use Bref to develop HTTP APIs for Lambda.

I had recently come across an issue where the response comes as a Base64 encoded string of the actual JSON response. I reverted back the bref version to 0.5.6 and I was getting plain JSON response. I havent changed anything in my PHP or API gateway configuration. Just the bref version from 0.5.7 to 0.5.6 and It was giving me plain JSON response.

I have come to report this and ask if the responses of version 0.5.7 are expected to be Base64 encoded strings or is this a Bug?

P.S. Your work made my life better. Otherwise I would have had to write a handler and other stuff for PHP APIs myself which is not an easy task. Thanks for everything.

bug

All 14 comments

Hi, indeed that sounds like a bug related to #457

Could you confirm that:

  • you had upgraded to 0.5.7
  • and you are using serverless.yml
  • and you are including the bref plugin in serverless.yml

I just want to make sure that you had the latest version of the code + the latest version of the runtime.

Thanks for the reply @mnapoli .

Yes,

  • I used composer to manage packages for that project and doing plain composer require bref/bref gave me the latest bref version then which was bref 0.5.7.
  • My Serverless.yml looks like this. This should make it easier for you to confirm.
    serverless-edited

  • I changed the bref version to 0.5.6 using composer without changing any project code or the serverless.yml and redeployed it using serverless and I got plain JSON response.

PS. I'll join the slack group and try to contribute too.

OK thanks for confirming.

My guess is that you return JSON but you don't set the HTTP response header Content-Type to application/json?
Just to be clear: if you don't, Bref should still work, so you are experiencing a bug.

Here is the relevant code I think:

https://github.com/brefphp/bref/blob/7361a9c0f1c0eb5a8619fe49195d6e97be62c6aa/src/Runtime/LambdaRuntime.php#L254-L257

We considered that API Gateway would always pick up a base64-encoded response and deal with it. But I will do some tests.

header("Content-Type: application/json; charset=UTF-8");
Even I thought same when I came across that Base64 string but I had it set. Okay, thanks for pointing to the code.

Oh, I didn't know API gateway handles Base64 encoded response. I'll read up a little more on it.

Oh that's interesting, we detect application/json but not application/json; charset=UTF-8. We should probably fix that by matching the beginning of the string.

However still, even if we don't detect the header correctly things should work and you should not get base64, so we should fix the underlying issue as well.

Yeah I see that in code.

Can you share new invite to the slack workspace the one given on bref page says no longer available or something of sort.

Thanks.

@gouthampro3 thanks for the tip, I have fixed the link: https://bref.sh/docs/community.html

Also seeing the same issue with Yii 2.0. When I use the asJson($data) helper on the controller, it returns base64 encoded. Here is the code from the Yii framework: https://github.com/yiisoft/yii2/blob/master/framework/web/Controller.php#L71

Looks like Yii is also setting the application/json; charset=UTF-8 Content-Type header, which causes the issue.

https://github.com/brefphp/bref/blob/5ee5f6382bfbb9797d2c22565243150a34d666c8/src/Runtime/LambdaRuntime.php#L254

Having it as the following should fix the issue:

 if ($contentType && strpos($contentType, 'application/json') !== 0 && strpos($contentType, 'text/') !== 0) { 

Thanks for updating slack link on request @mnapoli.
@samdark I will change it and run some tests and let you know if that works for me too as soon as possible. Thanks for the help.

I just made this change locally and deployed here: https://q9xx2b9jo4.execute-api.us-east-1.amazonaws.com/dev

Here is the code for the controller:

<?php

namespace micro\controllers;

use yii\web\Controller;

class SiteController extends Controller
{
    public function actionIndex()
    {
        return $this->asJson(["hello" => "world"]);
    }
}

This is now working as expected!

Thanks @jasonmccallister for the PR.

I have merged it, I will tag a new release shortly. I will keep this issue open however as I plan on implementing a solution that will cover every other use case.

In any case, everyone that interacted in this issue should see the problem resolved regarding JSON responses.

I have opened #505 with a fix.

Bref 0.5.11 is released and should fix that definitively.

Was this page helpful?
0 / 5 - 0 ratings