Skip to content
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

[7.x] Add HTTP client response body as object #32341

Merged
merged 2 commits into from
Apr 13, 2020
Merged

[7.x] Add HTTP client response body as object #32341

merged 2 commits into from
Apr 13, 2020

Conversation

nuernbergerA
Copy link
Contributor

This PR lets you access the Http client response body as an object.

I also added tests to cover existing ways to access the response body.

Current state

// Current magic way - uses ->json() under the hood
$response = Http::get('some-api.wip');
$response['result'];

// Or with json()
$response = Http::get('some-api.wip')->json();
$response['result'];

Initially, I thought to pass a boolean to json() to tell if it should convert JSON as array or object.

But the fact that json() is used to implement ArrayAccess, and json(true) isn't verbose at all, I went with object()

Usage

// New option
$response = Http::get('some-api.wip')->object();
$response->result;

Future

As most API's wrap there responses with data or result.

// I thought it would be nice to do this
$response = Http::get('some-api.wip')->object('result');

But didn't implement it yet to keep it simple for now.

I hope you find it useful.
Happy Easter to everyone.

@nuernbergerA nuernbergerA changed the title [7.x] Add HTTP client response to object [7.x] Add HTTP client response body as object Apr 12, 2020
@taylorotwell taylorotwell merged commit 8975bd1 into laravel:7.x Apr 13, 2020
@mreduar
Copy link

mreduar commented Apr 19, 2020

Great! Thanks for this!

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.

None yet

3 participants