What about just adding your URI to the VerifyCsrfToken.php within app/Http/Middleware in the $except method?
Axios without CSRF and Requested with headers
Hi,
Using Laravel 5.5 and vue (I'm weak at JS) I'm making some axios requests that run both internally and externally to other content. The internal content is fine and needs CSRF tokens to protect our system. However the same CSRF are applied to external calls.
is there a way to keep using window.axios for internal calls and some other instance of axios kept for external requests?
Right now to get the externals working i've commented out the following:
bootstrap.js
window.axios = require('axios');
// window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
//
// let token = document.head.querySelector('meta[name="csrf-token"]');
//
// if (token) {
// window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
// } else {
// console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
// }
That solves all the external issues but removes protection on the system.
How can I make something like externalAxios to make external calls while keeping axios for internals?
Thanks in advance for your time.
humm, I guess I'm stupid I thought external ajax calls were still limited to the host server. (unless pjax). Nope I'm not stupid. You can't do external ones. (Unless it's the same host, is it?).
Now if it is the same host, but not laravel. What error are you getting for the 'external' class when you include the CSRF token?
That said, you can remove the CSRF token on your axios request just before you call axios.get or post. do this: delete window.axios.defaults.headers.common['X-CSRF-TOKEN'];
after do this: window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
if you are doing mostly 'external' calls. remove this in bootstrap.js window.axios.defaults.headers.common['X-CSRF-TOKEN']
then for your internal calls add them for the request.
var config = {
headers: {'X-My-Custom-Header': 'Header-Value'}
};
axios.get('https://api.github.com/users/codeheaven-io', config);
Please or to participate in this conversation.