HTTP Interceptor vs Proxy Interceptor: Angular, Axios and Fetch Compared
Code interceptors belong to your app. Proxy interceptors belong to your debugging session. Mixing them up is how mock data ends up in production.
Key takeaways
- Put permanent behaviour (auth headers, token refresh, logging, error mapping) in code interceptors.
- Put temporary scenarios (mocks, failures, delays, redirects) in a proxy interceptor, outside the codebase.
- Never ship a mock interceptor behind a feature flag.
Angular HttpInterceptor
Modern Angular uses functional interceptors registered with provideHttpClient.
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).token();
return next(token ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req);
};
// app.config.ts
provideHttpClient(withInterceptors([authInterceptor]));Axios interceptors
api.interceptors.response.use(undefined, async (error) => {
const original = error.config;
if (error.response?.status === 401 && !original._retried) {
original._retried = true;
await refreshSession();
return api(original);
}
return Promise.reject(error);
});Fetch has no interceptor API
The Fetch API has no built-in hooks, so teams write a wrapper such as apiFetch() and use it everywhere. Wrapping window.fetch globally is possible, and it is exactly what browser interceptor tools do internally, but it is fragile in application code.
Where a proxy interceptor fits
Now suppose you need to see how the UI above behaves when the refresh endpoint returns 500, or when /me takes 8 seconds. You *could* add a mock interceptor, but then you have to commit, build and remember to remove it. A proxy interceptor makes the same change from outside the code, only in your browser, and your real interceptors still run so you are testing the true behaviour.
{
"name": "Refresh endpoint fails",
"match": { "urlPattern": "*/auth/refresh", "matchType": "wildcard" },
"response": {
"body": { "enabled": true, "mode": "replace-whole", "statusCode": 500, "value": "{}" }
}
}Decision table
| Task | Where it belongs |
|---|---|
| Attach auth header | Code interceptor |
| Refresh expired token | Code interceptor |
| Global error toast | Code interceptor |
| Test what happens when refresh fails | Proxy interceptor |
| Demo a feature before the API exists | Proxy interceptor |
| Reproduce a customer's exact payload | Proxy interceptor |
| Unit tests | Test-level mocks (MSW, HttpTestingController, Jest) |
Frequently asked questions
Can a proxy interceptor see requests made by Angular HttpClient?
Yes. HttpClient uses XHR (or fetch with withFetch()), and ProxyCeptor intercepts both in the page.
Do proxy interceptor rules run before or after my Axios interceptors?
After your request interceptors (the browser sees the final request) and before your response interceptors (your code receives the modified response). That means you are testing your real interceptor logic.
Is MSW a proxy interceptor?
Mock Service Worker intercepts at the service-worker level and is configured in code, so it sits between the two: ideal for tests, less convenient for ad-hoc debugging. See ProxyCeptor vs MSW.
Read next
Intercept your first request in under a minute
Create a free ProxyCeptor account to mock, delay, block and rewrite API traffic, then share the same rules with your team.