Interface Definition
Functional Interceptor
Angular also provides a functional approach withHttpInterceptorFn:
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
Functional Interceptor (Recommended)
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
UsewithInterceptors() to provide functional interceptors:
Class-based Interceptors
UseHTTP_INTERCEPTORS token to provide class-based interceptors:
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
Use Functional Interceptors
Use Functional Interceptors
Prefer functional interceptors (
HttpInterceptorFn) over class-based interceptors for better tree-shaking and simpler code.Keep Interceptors Focused
Keep Interceptors Focused
Each interceptor should have a single responsibility. Create multiple interceptors rather than one complex interceptor.
Always Call next()
Always Call next()
Always call
next() (functional) or next.handle() (class-based) unless you intentionally want to block the request.Clone Before Modifying
Clone Before Modifying
Requests are immutable. Always clone the request before modifying it.
Handle Errors Gracefully
Handle Errors Gracefully
Use proper error handling to prevent the interceptor chain from breaking.
Be Aware of Order
Be Aware of Order
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
Related Types
- HttpRequest - Represents an HTTP request
- HttpResponse - Represents an HTTP response
- HttpHandler - Interface for the next handler
- HttpEvent - Union type for HTTP events
- HttpErrorResponse - Represents an HTTP error
