[Axios] How to extract header data in Axios response interceptor
Let me give an example first. Frontend sends an API request, and server will reject it and add a reason of the rejection into the response header. In this scenario, Frontend wants to extract the reason from the header in the response interceptor and then navigate to different page depending on the reason. The code is very straightforward like below. 1export function errorInterceptor(error: AxiosResponse) { 2 if (error.status === 401) { 3 const reason = error.headers['reason']; 4 if (reason === 'bar') { 5 window.location.href = '/#!/foo'; 6 } 7 } 8 return Promise.reject(error); 9} 10 11axios.interceptors.response.use( 12 (response: AxiosResponse) => response, 13 errorInterceptor 14); Unfortunately, the above code will behave unexpectedly. As the highlight code error.headers['reason'] will be undefined. ...