'How to ignore interceptors in axios as a parameter?
I have axios interceptors, is it possible to ignore interceptors for one request by invoke the axios method?
something like: axios.post('/path/foo', null, { ignoreInterceptors: true } })
axios.interceptors.response.use(
(response) => {
return response;
},
(error) => {
return Promise.reject(error);
}
);
Solution 1:[1]
Instead of:
axios.post('/path/foo')
you do:
const uninterceptedAxiosInstance = axios.create();
uninterceptedAxiosInstance.post('/path/foo')
and nothing will intercept that request.
Solution 2:[2]
In the interceptor request you can check for an overwrite like so:
axiosInstance.interceptors.request.use(
(config) => {
const accessToken = getAuthToken();
config.headers!["Authorization"] =
config.headers && config.headers["Authorization"] !== undefined
? config.headers["Authorization"]
: accessToken;
config.headers!["Content-Type"] = "application/json";
config.headers!["Accept"] = "application/json";
return config;
},
(error) => Promise.reject(error)
);
so if you want to overwrite the authorization token in a single request but still you want to use the same axios instance you can make a request like that:
axiosInstance.get(`/endpoint`, {
headers: { Authorization: "" },
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Biskrem Muhammad |