Skip to main content
HTTP interceptors allow you to intercept and transform HTTP requests and responses globally in your Angular application. They’re useful for adding authentication tokens, logging, error handling, caching, and more.

Interface Definition

Functional Interceptor

Angular also provides a functional approach with HttpInterceptorFn:

Importing

Methods

intercept()

Intercepts an HTTP request and handles it.
HttpRequest<any>
required
The outgoing request object to handle
HttpHandler | HttpHandlerFn
required
The next interceptor in the chain, or the backend if no interceptors remain
Observable<HttpEvent<any>>
An observable of the HTTP event stream

Usage Examples

The functional approach is more lightweight and works with Angular’s injection context:

Class-based Interceptor (Legacy)

The traditional class-based approach:

Providing Interceptors

Functional Interceptors

Use withInterceptors() to provide functional interceptors:

Class-based Interceptors

Use HTTP_INTERCEPTORS token to provide class-based interceptors:
Or with standalone APIs:

Interceptor Chain

Interceptors are called in the order they are provided:
Each interceptor must call next() to pass the request to the next interceptor. If an interceptor doesn’t call next(), the request chain is broken and the request won’t reach the server.

Request Transformation

Requests are immutable, so you must clone them to make changes:

Common Transformations

Response Transformation

Transform responses using RxJS operators:

Conditional Interception

Apply interception logic conditionally:

Error Handling

Handle errors at the interceptor level:

Best Practices

Prefer functional interceptors (HttpInterceptorFn) over class-based interceptors for better tree-shaking and simpler code.
Each interceptor should have a single responsibility. Create multiple interceptors rather than one complex interceptor.
Always call next() (functional) or next.handle() (class-based) unless you intentionally want to block the request.
Requests are immutable. Always clone the request before modifying it.
Use proper error handling to prevent the interceptor chain from breaking.
Interceptors execute in the order they’re provided. Order matters for operations like authentication and logging.

Common Use Cases

Authentication

Add authentication tokens to outgoing requests

Logging

Log all HTTP requests and responses

Error Handling

Centralized error handling and recovery

Loading States

Show/hide loading indicators

Caching

Cache responses for improved performance

Retry Logic

Automatically retry failed requests

Request Transformation

Modify requests before sending

Response Transformation

Transform responses before consumption

See Also

HTTP Overview

Introduction to Angular’s HTTP client

HttpClient

Complete API reference for HttpClient