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

Community
  • 1
  • 1
NEOJPK
  • 757
  • 1
  • 8
  • 17
  • I have the same error, did you find any solution? – Nenad Mar 09 '17 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. – NEOJPK Mar 09 '17 at 12:50
  • I made a gist for you explaining the whole thing https://gist.github.com/pilsoftTeam/093980487651a27d28df034dbd27a3fb – NEOJPK Mar 09 '17 at 12:59

1 Answers1

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;
});
craig_h
  • 31,871
  • 6
  • 59
  • 68
  • But does this explains why i can do GET calls even with axios ? But POST with any of them – NEOJPK Jan 19 '17 at 20:32
  • 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. – craig_h Jan 19 '17 at 20:42