Support the ongoing development of Laravel.io →
Requests Testing

I've read several threads where people had problems testing their controllers by posting json to them - I tried the same and cannot get it to work as well...

What I'm trying to do is:

$data = json_encode([...some-data...]);
 $response = $this->call(
            'POST', 'api/v1/handle', [], [], [], [], $data);

When I debug in the corresponding controller...

 public function store(Request $request)
 {
        $data = $request->all();
 }

... I can see the data (json string) is set in the Request::content property but its not available in $data when trying to retrieve it with $request->all(). When I POST from the Javascript in the frontend, which is sending plain json in the request payload everything is available from $request->all() . Whats the missing point on using ApplicationTrait::call() ?? I can't see it.

Last updated 3 years ago.
0

Okay.. I found it.

// In the controller

$data = $request->json();

It works now for both, making requests from the browser or the test class...

0

Sign in to participate in this thread!

Eventy

Your banner here too?

mk-conn mk-conn Joined 1 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.