Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

algae14's avatar

API route returning 404, but Web route works

So, I have two routes setup. One route is in web.php.

Route::post('/trials', 'TrialsController@store');

And the API version of it is in api.php:

Route::post('/v1/trials', 'TrialsController@store');

I created a simple form for myself to test the web route from the browser, and can successfully store a trial by POSTing to the web route above. However, from Postman when I try to store a trial by POSTing to the API route above, I get a 404 in the response.

Here is my TrialsController's store method.

{
    // Here we are after successful validation of the request

    // First, we create the firm associated with the trial. 
    $firm = Firm::create([
        'name' => $request->name,
        'aum' => $request->aum,
    ]);

    // Then, we create a new contact in the database for the firm.
    $contact = $firm->contacts()->save(new Contact([
        'first_name' => $request->first_name,
        'last_name' => $request->last_name,
        'email' => $request->email,
        'phone' => $request->phone
    ]));

    // Finally, we create the new initial trial in the database for the contact.
    $trial = $contact->trials()->save(new Trial([
        'starts_on' => $request->starts_on,
        'ends_on' => $request->ends_on,
        'repeated' => 0
    ]));

    // All is saved to the database, now return the new intial trial.
    return (new TrialResource($trial));
}

The StoreTrial form request rules' method is as follows.

{
    return [
        'name' => 'required | min:3',
        'aum' => 'numeric',
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required',
        'phone' => 'required',
        'starts_on' => 'required',
        'ends_on' => 'required'
    ];
}

Is this related to special authentication that I need to setup for API calls? I am a bit lost as to why web route works but api route does not. Can two routes not hit the same controller/method? Appreciate any help. Thanks!

0 likes
5 replies
Cronix's avatar

You sent the api request to /api/v1/trials? The api group (api.php) automatically prepends /api to the api routes.

algae14's avatar

@CRONIX - Yes, with Postman I'm sending the POST request to /api/v1/trials. How else would Laravel know that I am using the "api" route instead of the web route?

Also, when I remove /api/v1 from the URI, the Postman response is a 419.

algae14's avatar

@CRONIX - Is the real problem that I'm hitting the same controller and method with two different routes, a web route and an api route?

Cronix's avatar
Cronix
Best Answer
Level 67

How else would Laravel know that I am using the "api" route instead of the web route?

How are we supposed to know you're hitting the correct route? You don't show any code showing the actual url you are hitting. You say you are, but you're also getting a 404 so the first logical step is to find out if you were actually hitting the correct endpoint.

algae14's avatar

@CRONIX - Fair enough. :-)

I was definitely hitting the correct endpoint. That's why I was confused enough to post this question.

Realized my issue was user error within Postman (of course). I did not have the content-type= application/json set in the header. Once I added that to the header in Postman, it worked.

Thanks for your help! It got me looking in the right place.

1 like

Please or to participate in this conversation.