NG.VII · Angular 22 practice question
Consider the following setup where a class interceptor logs the request URL and adds a custom header. It is registered with `provideHttpClient(withInterceptorsFromDi())` (or rewritten as an `HttpInterceptorFn` and registered with `withInterceptors()`). ```typescript import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable() export class LoggingInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<any> { console.log('Request URL:', req.url); const modifiedRequest = req.clone({ setHeaders: { 'X-Custom-Auth': 'token123' } }); return next.handle(modifiedRequest); } } ``` If this interceptor is registered correctly, what will happen when a service makes an HTTP call?
Check answer and explanation
Correct answer: The console will log the request URL, and the outgoing request will contain the `X-Custom-Auth` header.
Explanation
The `HttpRequest` object has a `clone()` method which creates a copy of the request, allowing immutable modifications like adding or changing headers. The logging action happens synchronously before `next.handle()`, and the modified request is passed downstream.
Common trap
Options A and C reference incorrect assumptions about the API structure or lifecycle. Option D references a non-existent deprecation in this context.
Source: HTTP Interceptors Guide - https://angular.dev/guide/http/interceptors
Checked against the cited text on 2026-11-27.