2

I have a problem with the http calls in my api.

The thing is that i have this http POST call:

axios.post("/api/crear/dimension/", data).then((response) => {
                        this.success();
                        console.log(response.data);
                    }).catch((response) => {
                        this.error(response)//;
                    })

Using axios because vue resource is getting deprecated.

the thing is that when i want to trigger the call this happens :

GET http://localhost/api/crear/dimension 405 (Method Not Allowed)

Notice how chrome takes this as GET call instead of a POST one. IDK why

But when i do a GET call everything works fine.

this is my route file in laravel:

Route::group(['middleware' => 'auth:api'], function () {
  Route::get('/user', function (Request $request) {
    return $request->user();
  });

  Route::post('/crear/dimension/', 'AdminController@createDimension');

});

PS : Im using the passport package in laravel. Everything is configured well because i can get a GET call with no problems. The thing is in the POST call,

I did a put , patch call too. Same result as above,

i have tried with vue resource aswell but nothings works

//EDIT

I removed the slashes from the end of the api calls and i got a 401. For more info go HERE

3
  • I have the same error, did you find any solution? Commented Mar 9, 2017 at 10:47
  • 2
    yes. You have to remove the last slash in the api call in laravel and in your api call with axios or whatever you using. Commented Mar 9, 2017 at 12:50
  • I made a gist for you explaining the whole thing gist.github.com/pilsoftTeam/093980487651a27d28df034dbd27a3fb Commented Mar 9, 2017 at 12:59

1 Answer 1

1

vue-resource isn't being deprecated, it's simply no longer officially supported by Vue. Laravel automatically defines an interceptor for vue-resource so the csrf_token is automatically passed, you will find the following in resources/assets/js/bootstrap.js:

Vue.http.interceptors.push((request, next) => {
    request.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;

    next();
});

If you are using a different ajax library then you will need to pass this csrf_token. I don't use axios but from their docs you will probably need to include something like this in your bootstrap.js file:

axios.interceptors.request.use(function (config) {
   config.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;
   return config;
});
Sign up to request clarification or add additional context in comments.

2 Comments

But does this explains why i can do GET calls even with axios ? But POST with any of them
A post requires a csrf_token in laravel otherwise you will receive a 405 error, I'm not sure why chrome is telling you it's a GET, I assume axios is well tested, so it may just be something internal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.