Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

How do you handle HTTP requests in Angular, and what are interceptors used for?

HttpClient returns a cold observable — the request is not sent until something subscribes, and it emits once then completes. Binding it through the async pipe is usually cleaner than subscribing manually.

getOrders(): Observable<Order[]> {
  return this.http.get<Order[]>('/api/orders').pipe(
    retry({ count: 2, delay: 1000 }),
    catchError(this.handleError)
  );
}

Interceptors sit in the middle of every request and response, so cross-cutting concerns live in one place instead of in every service:

  • Attaching an auth token to outgoing requests.
  • Refreshing an expired token on a 401 and retrying the original request.
  • Centralised error handling — turning HTTP errors into a toast or a redirect.
  • A loading indicator driven by a count of in-flight requests.
  • Logging and correlation ids.

Note: Requests are immutable, so an interceptor must clone rather than mutate: req.clone({ setHeaders: { Authorization: token } }). Interceptors also run in the order they are registered, which matters when one adds a header another depends on.

All Angular interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as