Skip to content

HTTP client response json method without key argument doesn't respect default argument #60029

@michaeldzjap

Description

@michaeldzjap

Laravel Version

13.8.0

PHP Version

8.5.5

Database Driver & Version

No response

Description

A call to Illuminate\Http\Client\Response#json(default: []) does not respect the passed in default value. In other words: if the result of the aforementioned call is null this will be the value that will be returned instead of []. This is not logical in my opinion.

Whether this is a bug or not is perhaps debatable. From my (API consumer) perspective it is at least, as it doesn't make sense that passing in a key argument does respect the passed in default. I would at least expect it to be noted in the HTTP client documentation somewhere.

A fix seems easy enough: just replace return $this->decoded; with return $this->decoded ?? $default; here. Although you could argue that this will perhaps introduce a breaking change.

Steps To Reproduce

$response = Http::get(...);

// Assuming there is no response payload this returns `null` instead of `[]`, which doesn't seem logical
$response->json(default: []);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions