Laravel 5.2: POST request is always returning "405 Method Not Allowed"
I have same problem with you which are I already set in my POST route for example "/api/v1/user",
and when I try to connect using POSTMAN (application to test API) , it return 405-Method Not Allowed,
and then i realize the url i was sent is using 'http' and after i change it to 'https' (with the 's' at the back)
then my API working like normal !
normally if we interact with different server , we must use 'https'
but if your application at the same server ,
it's okay to use 'http'
The real reason for my case is any interaction with any different server must use 'https' (This is the setup on my server)
You need to separate your routes because all the users trying to get to your routes need a open session (logged in)
Try this
Route::group(array('prefix' => 'api/v1'), function() { Route::post('/','UserController@store'); Route::get('/', 'UserController@index'); Route::group(array('before' => 'auth.basic'), function() { Route::post('{user}', 'UserController@update'); });});
Your authorized users routes should be in the second group
And your 405 Method not Allowed is $user->id
change it for $request->user()->id
Missing CSRF token
I had the same issue on Laravel 5.8 when creating web-hooks
routes.
These routes use the web middleware and because of that, the VerifyCsrfToken route middleware group is also included. (Reference app/Http/Kernel.php
)
Because my POST request doesn't include a CSRF token we get this strange behaviour.
Add CSRF exception
To fix this, I needed to add an exception to the VerifyCsrfToken middleware for the web-hooks routes. (Reference app/Http/Middleware/VerifyCsrfToken.php
)
/** * The URIs that should be excluded from CSRF verification. * * @var array */protected $except = [ 'web-hooks/*'];
Use API middleware
The above solution is for when the web middleware is used. However if you are creating API routes, it is better to use the api middleware instead because no CSRF verification is used in this middleware. (Reference app/Providers/RouteServiceProvider.php
)
Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php'));