-
Notifications
You must be signed in to change notification settings - Fork 46
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
getParsedBody() not working in slim v4 #99
Comments
This probably requires adding a middleware to parse |
There's already extensive handling for json bodies in |
Ah yes, I see. The decorator is missing the handling for parsing JSON bodies. |
Well not exactly. The issue is that Nyholm\Psr7Server does not return In php, when Content-Type is application/json $_POST is [] not null. |
@esetnik this is a tough problem. We could perhaps make a method which forces the body parsing no matter what the existing parsed body is? My concern with removing the null check is do we want to interfere with the underlying implementations’s parsed body and completely override it? |
Right. And I made a change which fixed the issue for me. $parsedBody = $this->serverRequest->getParsedBody();
if (!empty($parsedBody)) {
return $parsedBody;
} But it's weird because this test case is already passing and fails with my change, whereas my sample project doesn't work. So I need to understand why this test case is behaving differently from my project. |
The problem with using |
I agree, a null check is the appropriate way to ensure the body has not already been parsed by the implementation. It looks like the real issue might be the requirement of slim v4 to use Nyholm/psr7-server which passes an empty array for parsedBody in all cases. The reason the test cases still pass is that they don't actually test the Unfortunately it looks like it's also broken in |
@esetnik using What we can do however is provide a workaround in Slim-Http so you can force body parsing using the logic in this repo if needed since we will never be able to accommodate all the different implementations’ quirks. |
Yeah the thing is that there's currently no option that I found which works with slim v4 and slim-http. I've tried each of the compatible factories and they all pass empty array not null. So there's currently no way to use |
I think part of the confusion is that the psr7 standard says that
|
I’m fine with using |
@l0gicgate I opened #100 for you to review. Thanks for the feedback. |
The same issue affects GuzzleHttp's PSR-7 implementation. It appears numerous "correct" implementations of PSR-7 demonstrate this same tendency, and the PR in question is a quick and easy solution. |
In https://github.com/slimphp/Slim-Http/blob/master/src/ServerRequest.php#L165
The
$parsedBody
is checked against null. When a POST request is received withContent-Type: application/json
the php server will set$_POST
to an empty array.In
https://github.com/Nyholm/psr7-server/blob/master/src/ServerRequestCreator.php#L54
https://github.com/Nyholm/psr7-server/blob/master/src/ServerRequestCreator.php#L75
The serverRequest $parsedBody is initialized to the empty array and fails the check above so no parsed body data is returned from
$request->getParsedBody();
.The text was updated successfully, but these errors were encountered: