This interceptor adds a bearer token to every HTTP request's headers automatically. You register it in your app module so Angular uses it for all requests.
C. next.handle should be called with a cloned request
D. Headers cannot be set in interceptors
Solution
Step 1: Check method existence on req
The HttpRequest object is immutable and does not have a setHeaders method.
Step 2: Understand correct way to set headers
Headers must be set by cloning the request with new headers using req.clone().
Final Answer:
req.setHeaders is not a function -> Option B
Quick Check:
setHeaders method does not exist [OK]
Hint: HttpRequest is immutable; no setHeaders method [OK]
Common Mistakes:
Trying to call setHeaders directly
Ignoring immutability of HttpRequest
Thinking headers can't be changed at all
5. You want to add a custom header 'X-App-Version' with value '1.0' to all outgoing HTTP requests in your Angular app. Which is the best way to do this?
hard
A. Add the header manually in every HTTP call
B. Modify the HttpClient service to include the header
C. Create an interceptor that clones requests and sets the header, then register it globally
D. Use a component to add the header before each request
Solution
Step 1: Understand global header addition
Manually adding headers everywhere is error-prone and repetitive.
Step 2: Use interceptor for centralized modification
Interceptors allow modifying all requests in one place by cloning and setting headers, then registering globally.
Final Answer:
Create an interceptor that clones requests and sets the header, then register it globally -> Option C
Quick Check:
Global header addition = interceptor + clone + register [OK]
Hint: Use interceptor to add headers globally [OK]
Common Mistakes:
Adding headers manually in every call
Trying to modify HttpClient directly
Using components for HTTP header changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.