An HTTP interceptor is a service that can inspect and transform HTTP requests or responses before they are handled by the application. It acts like a middleman to modify requests or responses globally.
Click to reveal answer
beginner
How do you register an HTTP interceptor in Angular?
You provide the interceptor class in the providers array with the token HTTP_INTERCEPTORS and set multi: true to allow multiple interceptors.
Click to reveal answer
intermediate
What method must an Angular HTTP interceptor implement?
An Angular HTTP interceptor must implement the intercept(req: HttpRequest<any>, next: HttpHandler) method, which returns an Observable of the HTTP event stream.
Click to reveal answer
intermediate
Why use interceptors to modify HTTP requests instead of modifying requests in each service?
Interceptors allow centralized request modification, reducing repeated code and ensuring consistent behavior like adding headers or logging across all HTTP calls.
Click to reveal answer
beginner
Can Angular HTTP interceptors modify both requests and responses?
Yes, interceptors can modify outgoing requests before they are sent and also inspect or modify incoming responses before they reach the component.
Click to reveal answer
Which Angular token is used to register HTTP interceptors?
AHTTP_CLIENT
BHTTP_INTERCEPTORS
CHTTP_HANDLER
DHTTP_SERVICE
✗ Incorrect
HTTP_INTERCEPTORS is the Angular injection token used to register one or more HTTP interceptors.
What does the intercept method receive as parameters?
AHttpRequest and HttpHandler
BHttpResponse and HttpClient
CHttpEvent and HttpHeaders
DHttpClient and HttpInterceptor
✗ Incorrect
The intercept method receives the HttpRequest to modify and the HttpHandler to forward the request.
How do you ensure multiple interceptors can be used together?
ASet multi: true when providing interceptors
BUse only one interceptor at a time
CRegister interceptors in different modules
DUse different tokens for each interceptor
✗ Incorrect
Setting multi: true allows Angular to keep multiple interceptors in an array and apply them all.
Which of these is a common use case for HTTP interceptors?
AHandling user input events
BRendering UI components
CAdding authentication tokens to headers
DStyling HTML elements
✗ Incorrect
Interceptors often add authentication tokens or custom headers to outgoing HTTP requests.
What type does the intercept method return?
Avoid
BPromise<HttpResponse>
CHttpRequest
DObservable<HttpEvent<any>>
✗ Incorrect
The intercept method returns an Observable stream of HttpEvent objects.
Explain how to create and register an HTTP interceptor in Angular to add a custom header to all requests.
Think about modifying the request inside intercept and registering the interceptor in providers.
You got /5 concepts.
Describe why using HTTP interceptors is better than modifying HTTP requests individually in each service.
Consider the benefits of handling cross-cutting concerns in one place.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of an HTTP interceptor in Angular?
easy
A. To modify HTTP requests before they are sent
B. To create new HTTP requests from scratch
C. To handle user input events
D. To style components dynamically
Solution
Step 1: Understand the role of interceptors
Interceptors are designed to catch HTTP requests and responses to modify or handle them.
Step 2: Identify the correct purpose
Among the options, only modifying HTTP requests before sending matches the interceptor's role.
Final Answer:
To modify HTTP requests before they are sent -> Option A
Quick Check:
Interceptor purpose = modify requests [OK]
Hint: Interceptors change requests before sending [OK]
Common Mistakes:
Thinking interceptors create requests
Confusing interceptors with event handlers
Assuming interceptors style components
2. Which method is used to safely modify an HTTP request inside an Angular interceptor?
easy
A. req.clone()
B. req.change()
C. req.update()
D. req.modify()
Solution
Step 1: Recall Angular HTTP request immutability
HTTP requests are immutable, so you cannot change them directly.
Step 2: Use the clone method to modify
The correct way is to create a copy with changes using req.clone().
Final Answer:
req.clone() -> Option A
Quick Check:
Modify request = req.clone() [OK]
Hint: Use req.clone() to change requests safely [OK]
Common Mistakes:
Trying to modify request directly
Using non-existent methods like req.modify()
Confusing clone with update or change
3. Given this interceptor code snippet, what will be the value of the 'Authorization' header in the outgoing request?
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.