Loading

Quipoin Menu

Learn • Practice • Grow

angular / HTTP Interceptors
interview

Q1. What are HTTP interceptors in Angular?
Interceptors are middleware that can inspect and transform HTTP requests and responses. They can be used for adding authentication headers, logging, caching, or handling errors globally. Interceptors implement HttpInterceptor interface with intercept method.

Q2. How do you create an interceptor?
Implement HttpInterceptor. Example auth interceptor:
@Injectable() export class AuthInterceptor implements HttpInterceptor { intercept(req: HttpRequest, next: HttpHandler) { const authReq = req.clone({ headers: req.headers.set(''Authorization'', ''Bearer token'') }); return next.handle(authReq); } }

Q3. How do you register an interceptor?
Provide the interceptor in the module using HTTP_INTERCEPTORS token. Example:
providers: [ { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true } ]
multi: true allows multiple interceptors.

Q4. What is the order of multiple interceptors?
Interceptors are called in the order they are provided. The request goes through them in sequence, and the response comes back in reverse order. So if you have A then B, request goes A -> B, response goes B -> A.

Q5. What are common use cases for interceptors?
Common uses: adding authentication tokens, logging requests/responses, handling global errors, setting default headers, transforming request/response formats, caching, and showing a global loading spinner.