Discover how to make your app smarter by changing all requests with just one simple tool!
Why Interceptors for request modification in Angular? - Purpose & Use Cases
Imagine you have to add a security token to every single web request manually in your Angular app by editing each service call.
Manually adding tokens or headers to every request is repetitive, easy to forget, and makes your code messy and hard to maintain.
Interceptors let you automatically modify all outgoing requests in one place, so you never miss adding important info like tokens or headers.
http.get('/api/data', { headers: { Authorization: 'token' } }) http.post('/api/save', data, { headers: { Authorization: 'token' } })
An interceptor intercepts all requests and adds the Authorization header automaticallyYou can centrally control and modify all HTTP requests effortlessly, improving security and consistency.
Adding a login token to every API call without repeating code everywhere in your Angular app.
Manually modifying requests is error-prone and repetitive.
Interceptors automate request changes in one place.
This keeps your code clean and secure.
Practice
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 AQuick Check:
Interceptor purpose = modify requests [OK]
- Thinking interceptors create requests
- Confusing interceptors with event handlers
- Assuming interceptors style components
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 usingreq.clone().Final Answer:
req.clone() -> Option AQuick Check:
Modify request = req.clone() [OK]
- Trying to modify request directly
- Using non-existent methods like req.modify()
- Confusing clone with update or change
intercept(req, next) {
const cloned = req.clone({
setHeaders: { Authorization: 'Bearer token123' }
});
return next.handle(cloned);
}Solution
Step 1: Analyze the clone method usage
The code clones the request and sets the 'Authorization' header to 'Bearer token123'.Step 2: Understand header setting effect
The cloned request will have the header exactly as set, so the header value is 'Bearer token123'.Final Answer:
'Bearer token123' -> Option DQuick Check:
Header set to 'Bearer token123' [OK]
- Assuming header is missing
- Confusing header value with token only
- Thinking syntax causes error
intercept(req, next) {
req.setHeaders({ 'Content-Type': 'application/json' });
return next.handle(req);
}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 BQuick Check:
setHeaders method does not exist [OK]
- Trying to call setHeaders directly
- Ignoring immutability of HttpRequest
- Thinking headers can't be changed at all
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 CQuick Check:
Global header addition = interceptor + clone + register [OK]
- Adding headers manually in every call
- Trying to modify HttpClient directly
- Using components for HTTP header changes
Ready to prove it?
Pass the test and this pattern moves unknown
