Axios remove default headers for one request
·
Answer a question
I'm trying to get a location from the Google Maps API but i get a CORS Policy Error because i have these default headers in my bootstrap.js
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')
};
How do i remove them for a specific axios get request? I saw some answers were they used before the request
delete axios.defaults.headers.common["X-Requested-With"];
delete axios.defaults.headers.common["X-CSRF-TOKEN"];
But these will delete them entirely, any ideas? Thanks and regards!
Answers
You can use the transformRequest in the request you are making:
axios.get('xxxx.com', { transformRequest: [(data, headers) => {
delete headers.common.Authorization;
return data
}] })
Or you could delete the header directly and then add it back after:
delete axios.defaults.headers.common["Authorization"];
axios.get('xxxx.com');
axios.defaults.headers.common["Authorization"] = 'Bearer xxxx';
更多推荐

所有评论(0)