Skip to content

Conversation

@cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Jan 14, 2026

I really want to be able to specify that when I'm parsing JSON from a response, it should throw when receiving invalid JSON.

How to use

Call-site

$response = Http::get('https://some-api.example.com/users/123')->json(flags: JSON_BIGINT_AS_STRING);
assert($response['someBigId'] === '123343343580999843483023');

Additionally, these flags can also be passed to the object(), collect(), and fluent() methods.

$response = Http::post(
    'https://some-api.example.com/story',
    [
        'title' => 'Laravel Forever?',
        'body' => 'Not a question, a statement of fact.'
    ]
)->object(flags: JSON_THROW_ON_ERROR);

Setting a global default

You may wish to set an application level rule for how JSON is decoded. You do not have to remember to pass flags at the call site, as it will default to the application level defaults. To employ, add something like the following to your AppServiceProvider:

public function boot() {
    Request::$defaultJsonDecodingFlags = JSON_THROW_ON_ERROR;
}

// Inside your code
Http::get('https://laravel.com')->json(); // will throw a \JsonException

// Maybe we want to override the default
Http::get('https://laravel.com')->json(flags: 0); // doesn't throw a JsonException, we just get back false because laravel.com returns unparsable JSON

Bonus: Remembering how you decoded the body

Say we have a chain of functions where a Response is decoded earlier, but now I want to ensure that it is indeed parseable JSON? We memoize the flags used when decoding, and when we make another call with differing flags, we decode the JSON again.

$response = Http::get('https://laravel.com');
assert(false === $response->json());
assert(false === $response->json()); // does not decode again since we have passed no different flags

$response->json(flags: JSON_THROW_ON_ERROR); // decodes again and ❌ throws JsonException here

Breaking Change ?

I don't think so. We have set default parameters. It is possible someone overrode the class I suppose and is setting the $decoded property without also setting the flags. I'm not totally sure how that would be a problem, but something I considered.

@cosmastech cosmastech marked this pull request as draft January 14, 2026 16:29
Copy link

@jaywengrow jaywengrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laravel is the best.

@cosmastech cosmastech changed the base branch from master to 12.x January 14, 2026 21:57
@cosmastech cosmastech changed the title [13.x] Allow setting flags for decoding json in the Http Client's Response [12.x] Allow setting flags for decoding json in the Http Client's Response Jan 14, 2026
@cosmastech cosmastech marked this pull request as ready for review January 14, 2026 23:03
@taylorotwell taylorotwell marked this pull request as draft January 15, 2026 19:33
@cosmastech
Copy link
Contributor Author

All ready for another review! 🫡

Kudos on the AI demo last night.

image

@cosmastech cosmastech marked this pull request as ready for review January 15, 2026 21:45
@taylorotwell taylorotwell merged commit 940da94 into laravel:12.x Jan 19, 2026
70 checks passed
@cosmastech cosmastech deleted the patch-36 branch January 19, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants