Skip to content

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

Merged
taylorotwell merged 2 commits into
laravel:7.xfrom
nuernbergerA:add-http-client-response-to-object
Apr 13, 2020
Merged

[7.x] Add HTTP client response body as object#32341
taylorotwell merged 2 commits into
laravel:7.xfrom
nuernbergerA:add-http-client-response-to-object

Conversation

@nuernbergerA

Copy link
Copy Markdown
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

mreduar commented Apr 19, 2020

Copy link
Copy Markdown

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.

3 participants