Testing API - $request->get() returns null

Posted 2 years ago by alexleonard

So I'm building up a small internal API for an app which will be used by Angular and am trying to write tests for it.

Previously I had setup a normal, non-api version of a simple user create/store, and everything works as normal there, and I can use $request->get() without issue.

public function store(UserRequest $request, User $user)
{
    dd($request->get('status'));
}

With an associated test using $this->call() I get the string dumped out as normal

but in my api, test, if I do $this->json(), suddenly $request->get() just returns null. eg

    $this->json(
        'POST',
        route('api.user.store'),
        [
            'status' => 'active',
            'first_name' => 'FirstName',
            'last_name' => 'LastName',
        ]
    )

I can still access the parameter by doing say $request->status instead of $request->get('status'), but feels a bit weird.

I had read somewhere that I was going to need to set my enctype on ajax POSTs to multipart/form-data but trying to override the json method from Illuminate\Foundation\Testing\Concerns\MakesHttpRequests seemed to cause other weird issues.

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.