62

In Laravel's unit test, I can test a JSON API like that:

$this->post('/user', ['name' => 'Sally'])
    ->seeJson([
        'created' => true,
    ]);

But what if I want to use the response. How can I get the JSON response (as an array) using $this->post()?

1
  • 6
    $this->getResponse()->getContent() might do the trick. Commented Aug 25, 2015 at 16:01

10 Answers 10

101

Proper way to get the content is:

$content = $this->get('/v1/users/1')->decodeResponseJson();
Sign up to request clarification or add additional context in comments.

5 Comments

I agree. Works in 5.4.
Works in 5.7 too.
6.3.3: throws an exception that decodeResponseJson() has been deprecated. Would check the doc but we all know how pointless that is...
The full list of methods you can use is in Illuminate\Foundation\Testing\TestResponse class (Laravel 6 lts), in addition to Docs.
Works for 8.40 too
53

Currently, in 5.3 this is working...

$content = $this->get('/v1/users/1')->response->getContent();

However, it does break the chain since response returns the response and not the test runner. So, you should make your chainable assertions before fetching the response, like so...

$content = $this->get('/v1/users/1')->seeStatusCode(200)->response->getContent();

2 Comments

Using in Laravel 7, decodeResponseJson is the right method. This returns string.
How can i see what methods are available for response?
17

As at Laravel 8, this worked for me. I was returning an automatically generated field (balance) after the POST request has created the entity. The response was in the structure {"attributes":{"balance":12345}}

$response = $this->postJson('api/v1/authors', [
    'firstName' => 'John',
    'lastName' => 'Doe',
])->assertStatus(201);

$balance = $response->decodeResponseJson()['attributes']['balance'];

decodeResponseJson will pick the response and transform it to an array for manipulation. Using getContent() returns json and you will have to use json_decode on the returned data to turn it into an array.

1 Comment

Works in Version 8.+
13

I like to use the json method when working with json, instead of ->get()

$data = $this->json('GET', $url)->seeStatusCode(200)->decodeResponseJson();

Comments

9

I hit a similar problem and could not get $this->getResponse()->getContent() working with the built in $this->get() method. I tried several variations with no success.

Instead I had to change the call to return the full http response and get the content out of that.

// Original (not working)
$content = $this->get('/v1/users/1')->getContent();

// New (working)
$content = $this->call('GET', '/v1/users/1')->getContent();

1 Comment

The test case doesn't have a getContent() method. The get() method is a fluent helper that returns the test case (so you can chain multiple assertions). The test case does have a response property that will return your response object (instead of the test case). Then you can call getContent() on that response object. See my answer for more details.
7

Simple way:

$this->getJson('api/threads')->content()

2 Comments

Use json() here instead of content() to also json decode the response data
Also you can use getOriginalContent(), as it return in array.
6

Found a better way:

$response = $this->json('POST', '/order', $data);
$responseData = $response->getOriginalContent(); // saves the response as an array
$responseData['value']  // you can now access the array values

This method returns the response json as an array.

Comments

5

Just want to share, I have used the same in $this->json() like:

$response = $this->json('POST', '/order', $data)->response->getContent();

But I added one more line to use json response and decode otherwise decodeResponseJson() was not working for me.

$json = json_decode($response);

2 Comments

Might have been a version issue
Same! I prefer json_decode($response->getContent(), true)
0

You can just call:

$data = $response->json();

Comments

-2
$response = $this->json('POST', '/products', $data);
$data = $response->getData();

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.