Scott Cantrell
Scott Cantrell

Reputation: 107

Laravel Routing Always 404 on API Post

I'm in Laravel 5.8 in Windows trying to create an inbound API route. Postman always gives me a 404 when I try to hit this route:

http://MYHOSTISHERE/api/v1?event=NewDealerSetUp&api_key=MYKEYISHERE

CURL with

curl -X POST "http://rx-0-unicorn.local/api/v1?event=NewDealerSetUp&api_key=MYAPIKEYHERE"

also gives me a 404.

Can someone point me to what I'm doing wrong? I'm bald but I may start pulling out beard in a minute.

Details below, and thanks!

I have this route in app.php:

Route::post('v1?event={event}&api_key={api_key}', 'API\APIController@index');

The route shows in artisan route list:


|        | POST      | api/v1?event={event}&api_key={api_key}                                                                                                     |                                    | App\Http\Controllers\API\APIController@index                                             | api                        |


VerifyCsrfToken Middleware (just to see if it would work):

    protected $except = [
        'api/*'
    ];

Controller start:


namespace App\Http\Controllers\API;

use App\Models\Log\LogAPI;
use App\Models\Members;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;

class APIController extends Controller
{
    public function index($event, $api_key, Request $request)
    {
        $data = filter_var_array((array)$request, FILTER_SANITIZE_SPECIAL_CHARS);

        [...]

        if($event == 'NewDealerSetUp'){
            $setup = new NewDealerSetup();

            return $setup->newDealerSetup($request);
        }

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    
    # ------------------------------------------------------------------------------------------------------
    # NOTES: 
    # the log: strip per-dir prefix: C:/apache/htdocs/MYAPPNAME/public/ ->[EMPTY]  ... takes the ^[EMPTY] and returns an empty
    # ------------------------------------------------------------------------------------------------------

</IfModule>

Upvotes: 0

Views: 697

Answers (1)

Arun A S
Arun A S

Reputation: 7006

Your route definition is the issue here. You've used query parameters in the route definition v1?event={event}&api_key={api_key} which is wrong. I'm not sure of it, but I've never seen any route definitions like this before as in route definition, we usually only define the endpoint and don't give any query params

Your controller definition is

public function index($event, $api_key, Request $request)

For this to work, your route would need to be something like

Route::post('v1/{event}/{api_key}', 'API\APIController@index');

or anything similar like

Route::post('v1/event/{event}/api_key/{api_key}', 'API\APIController@index');

If you must use query parameters, then you will have to use

Route::post('v1', 'API\APIController@index');

and handle the params in your controller

public function index(Request $request)
{
    $event = $request->event;
    $api_key = $request->api_key;
    // Rest of code
}

Upvotes: 2

Related Questions