tristanbailey
tristanbailey

Reputation: 4605

Laravel POST route from an js call is returning 500

I updated Laravel composer update recently the move from 5.2.23 -> 5.2.31 looks to have changed something that means I am not understanding the error. Like the return type or syntax has changed.

My post route seems to not work now

Does this seem a valid POST route ?

Route::group(['as' => 'api::', 'namespace' => 'Api', 'prefix' => 'api'], function () {
        Route::post('/report', ['as' => 'reports.create', 'uses' => 'AuditReportController@create']);
});

It worked before I updated, now says the at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206

It could be the javascript posting an id to it, but that query looks good and it was laravel that changed when updated. console log : http://randomwebsite.co.uk/api/report 500 (Internal Server Error)

Tracking with some var_dump() in the RouteCollection.php it gets to getRouteForMethods(), so "$request->method() == 'OPTIONS'" is whats throwing it, but I don't know where this is set or if the main issue.

protected function getRouteForMethods($request, array $methods)                                       
{                                                                                                     
    if ($request->method() == 'OPTIONS') {                                                            
        return (new Route('OPTIONS', $request->path(), function () use ($methods) {                   
            return new Response('', 200, ['Allow' => implode(',', $methods)]);                        

        }))->bind($request);                                                                          
    }                                                                                                 

    $this->methodNotAllowed($methods);                                                                
}

full trace for the 500 error.

MethodNotAllowedHttpException in vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php line 219:

in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54

EDIT: Ok, found a way to get the error from the console more than 500. "TokenMismatchException in VerifyCsrfToken.php line 67:" The JS is passing a X-CSRF-Token:1218vsMzumAUJ1qZZF1laralarlar..etc so maybe a change there

return hash_equals((string) $request->session()->token(), (string) $token);

return hash_equals($sessionToken, $token);

my JS looks fine though, as the documentation

$.ajaxSetup({
     headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      }
});

Upvotes: 0

Views: 491

Answers (1)

tristanbailey
tristanbailey

Reputation: 4605

Ok, looks like it was a case issue after the update:

in the laravel class this seems to have forced a better string match:

OLD
return hash_equals((string) $request->session()->token(), (string) $token);
NEW
return hash_equals($sessionToken, $token);

So: 'X-CSRF-Token' should be 'X-CSRF-TOKEN' a case issue.

This lead me to solve a second issue that you can not pass an empty array to eloquent to save , need to keep it a json_encode([])

Upvotes: 0

Related Questions