Skip to content
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

JsonException in registerPsrRequest method - "Control character error, possibly incorrectly encoded." #48997

Closed
marekfri opened this issue Nov 13, 2023 · 4 comments · Fixed by #49047
Assignees

Comments

@marekfri
Copy link

Laravel Version

10.29.0

PHP Version

8.1.2

Database Driver & Version

No response

Description

We have updated laravel/framework to latest stable version and encountered an issue, that originally occurred in version 10.29.0. When communicating with server API, following error occurs:

Symfony\Component\HttpFoundation\Exception\JsonException
Could not decode request body.

It happens in Illuminate\Routing\RoutingServiceProvider class:

protected function registerPsrRequest()
    {
        $this->app->bind(ServerRequestInterface::class, function ($app) {
            if (class_exists(Psr17Factory::class) && class_exists(PsrHttpFactory::class)) {
                $psr17Factory = new Psr17Factory;

                return with((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
                    ->createRequest($illuminateRequest = $app->make('request')), fn ($request) => $request->withParsedBody(
                        array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
                    ));
            }

            throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.');
        });
    }

Functionality was OK until version 10.28.0, but all newer version are causing us this error. The issue is in the getPayload() method, which handles the data as JSON, but in our case it is not a JSON, hence the error. The change originated in this pull request: https://github.com/laravel/framework/pull/48696/files

We haven't found any information in the release notes / migration guide, which could help us fix this issue, so we assume this might be a bug to report. If this is not an error, please advise how to fix this on our side.

Link to Sentry issue: https://crystal-missions.sentry.io/share/issue/5c43f0cf13e34684bf845dec4e123197/

Thank you.

Steps To Reproduce

We are sending a gziped JSON data to our server API endpoint.

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@marekfri
Copy link
Author

Hi,

I've created the repository as suggested + created there a single controller with one method, which just tries to decode the gzipped json data from request body.
Link to repo: https://github.com/marekfri/laravel-bug-report

The issue is, I think, that in the getPayload() method, if data from getContent() is not empty, it is always tried to be json_decoded. But I don't think that we can just assume that the data will always be a json string.

Let me know if I can do anything further to help resolve this.

Thank you.

@timacdonald
Copy link
Member

Thanks for reporting this. We made some changes in this area due to some upstream breaking changes.

I’ll investigate this, likely on Monday, and see if we can get it fixed for you.

@timacdonald
Copy link
Member

I've created a fix for this. Thanks for reporting it.

#49047

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants