Laravel Route Post not allowed

- 1 answer

Ad

I'm trying to create a route post in laravel, i use "get" and it works fine but when i use "post", "delete" etc not working it's returning error 500 (Internal Server Error).

There is my route code

    Route::post('Register' ,function(){
    return "Hello World";
});

I'm using extension for google chrome "Advanced REST client" to execute one "post", and that gives me that information

Request headers 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json 
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: XSRF-TOKEN=


Response headers 
Host: localhost:60967
Connection: close
X-Powered-By: PHP/5.5.12
Cache-Control: no-cache, private 
date: Wed, 23 Dec 2015 01:51:29 GMT
Content-type: text/html

I'm searching for hours and i can't find a solution.

Ad

Answer

Ad

Your XSRF token is missing. By default, all routes in a fresh Laravel application have CSRF protection turned on.

You will either need to add a valid token to your POST request header, or to the POST data itself by setting _token.

If you simply need to test the POST route itself, you can temporarily disable the CSRF middleware, or apply it on a case-by-case basis.

To Disable
app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //\App\Http\Middleware\VerifyCsrfToken::class, //Comment this out
    ],
    'api' => [
        'throttle:60,1',
    ],
];
/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

To Enable as Route Middleware
app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    ],
    'api' => [
        'throttle:60,1',
    ],
];
/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'csrf' => \App\Http\Middleware\VerifyCsrfToken::class, //Move it here
];
Ad
source: stackoverflow.com
Ad