Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2: POST request is always returning "405 Method Not Allowed"

So I am developing an API with Laravel 5.2 and I'm facing an important issue.

I have a UserController that will manage the users of my app.

This is my routes.php file:

Route::group(array('prefix' => 'api/v1'), function() {    
   Route::post('user', 'UserController@store');
});

And I have my UserController defined like that:

class UserController extends Controller {

   public function index() {
       return 'Hello, API';
   }

   public function create(){
   }

   public function store(Request $request) {
       $user = new User;
       $user->email = $request->email;
       $user->password = $request->password;
       $user->fbId = $request->fbId;
       $user->ggId = $request->ggId;
       $user->firstName = $request->firstName;
       $user->lastName = $request->lastName;
       $user->imageUrl = $request->imageUrl;
       $user->country = $request->country;
       $user->mobile = $request->mobile;
       $user->gender = $request->gender;
       $user->client = $request->client;

       $user->save();

       return Response::json(array(
           'error' => false,
           'userId' => $user->id),
           200
       );
   }

   public function update(Request $request, $id) {
   }
}

And this is the output of php artisan route:list

+--------+--------+-------------+------+-------------------------------------------+------------+
| Domain | Method | URI         | Name | Action                                    | Middleware |
+--------+--------+-------------+------+-------------------------------------------+------------+
|        | POST   | api/v1/user |      | App\Http\Controllers\UserController@store | web        |
+--------+--------+-------------+------+-------------------------------------------+------------+

I'm using Postman to test my POST requests. Every time I make a POST request to /api/v1/user, I get a "405 Method Not Allowed" error.

Did I miss anything?

Is there anything I should do to fix this issue?

like image 485
Joseph El Khoury Avatar asked Jun 15 '16 13:06

Joseph El Khoury


4 Answers

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)

like image 137
Bathulah Mahir Avatar answered Oct 19 '22 22:10

Bathulah Mahir


Changing to https will cause it to work. At least, that is what I have experienced. Using POSTman with http always has this effect.

like image 20
anabeto93 Avatar answered Sep 20 '22 12:09

anabeto93


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

like image 8
Enmy Pérez Moncada Avatar answered Oct 19 '22 20:10

Enmy Pérez Moncada


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'));
like image 4
Bram Verstraten Avatar answered Oct 19 '22 22:10

Bram Verstraten