Reputation: 2939
My web routes are working, however I cannot POST to my api routes, I receive MethodNotAllowedHttpException
. I think this is a csrf token problem as GET works, but I can't figure out how to fix it. I am using Postman to simulate api requests.
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'devices',
],
],
RouteServiceProvider.php
protected function mapApiRoutes()
{
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
routes/api.php
Route::post('api', ['uses' => 'DeviceController@api']);
kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'auth:api',
'bindings',
],
];
This is an upgrade from an older version of Laravel that was working, I upgraded by installing a fresh copy of Laravel 5.4 then copied over my code, changing it as needed.
Upvotes: 0
Views: 8556
Reputation: 2939
Sorry I made this same mistake a month ago. The problem is that I wasn't using https. Its weird how I get MethodNotAllowedException for that, I guess that threw me off.
Upvotes: 0
Reputation: 2098
Send _token
value like this with POST request
$.ajax({
type: "POST",
url: "/your url",
data: {_token:$("input[name='_token']").val(),'other':'Other value'}
}).done(function( response ) {
....
});
Upvotes: 1