Axios 공식 홈페이지 따라하기

Axios 공식 홈페이지 따라하기 (7) - 인터셉터

vitamin3000 2025. 2. 5. 21:05

 

then 또는 catch로 처리되기 전에 요청과 응답을 가로챌 수 있다.

 

// 요청 인터셉터 추가
// 커스텀 인스턴스에서도 인터셉터 추가 가능
axios.interceptors.response.use(function (response) {
	// 응답 데이터가 있는 작업 수행
    return response
   	}, function (error) {
    	// 응답 오류가 있는 작업 수행
        return Promise.reject(error);
    });

 

나중에 필요할 때 인터셉터를 제거할 수 있다.

const myInterceptor = axios.interceptors.requeset.use(function () {/*>..*/});
axios.interceptors.request.reject(myInterceptor);