Description
Symfony version(s) affected
6.3.0
Description
Calling $request->request->get("param")
returns null
if the body is empty
Replacing it with $request->getPayload()->get("param")
throws a JsonException
under the same condition.
This is a big BC just for a name change.
The issue was reported in #50490 but it got no answer except documenting that getPayload()
throws a JsonException
.
How to reproduce
To reproduce, simply call $request->getPayload()->get("param")
or $request->getPayload()->all()
or even $request->getPayload()->has("param")
to check if a parameter was passed in the body when none was in fact passed.
Possible Solution
Unless we are supposed to keep using $request->request
and only infrequently use $request->getPayload()
for cases where we need to know the client failed to provide a body, I suggest to return an empty array.
public function toArray(): array
{
if ('' === $content = $this->getContent()) {
return [];
}
An exception should be used for exceptional events. An empty body is nothing but exceptional.
Additional Context
No response