Skip to content

[HttpFoundation] getPayload throws JsonException if request body is empty (again) #50647

Closed
@fmarchalemisys

Description

@fmarchalemisys

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions