user7747472
user7747472

Reputation: 1952

Laravel 5 GET api call is working but POST api calls are not working

How to disable CSRF verification on API routes in laravel?

In the API routes I am calling a function through POST and GET method. GET methord returning data's but POST method throwing method not found exception.

Here is my sample code in routes/api.php

Route::post('hellopostapi', function() {
    return json_encode( 'we are getting POST response');
});

Route::get('helloget', function() {
    return json_encode( 'we are getting GET response);
});

For GET call i am getting the response as expected. And for POST i am getting this exception

"message": "", "exception":"Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",

"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",

"line": 255,

"trace": [
    {
        "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",

        "line": 242,

        "function": "methodNotAllowed",

        "class": "Illuminate\\Routing\\RouteCollection",

        "type": "->"
    },
    {
        "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
        "line": 176,
        "function": "getRouteForMethods",
        "class": "Illuminate\\Routing\\RouteCollection",
        "type": "->"
    },

I also added api routes in verifycsrf.php , as per documentation it should work, unfortunatly its not working for me. here is my verifiycsrf.php

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*',
    ];
}

API Testing screenshot POST request which causing issue enter image description here

Get request which is working fine enter image description here That's still not working. Can anyone please help me .Thank you

Upvotes: 0

Views: 1682

Answers (2)

user7747472
user7747472

Reputation: 1952

Looking deeper into this, i found out that this was never a laravel issue, rather it was causing due to HTTP to HTTPS redirection. After changing http to https the problem seems to resloved. :)

Upvotes: 2

Leo Rams
Leo Rams

Reputation: 739

Check the form method you are posting from, it's highly likely that the method is GET method yet in your routes file you have configured it to expect a POST method

Upvotes: 0

Related Questions