0
0
Angularframework~10 mins

Interceptors for request modification in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interceptors for request modification
HTTP Request Initiated
Interceptor intercept() called
Modify Request (e.g., add header)
Forward Modified Request
Server receives modified request
Server sends response
Interceptor intercept() receives response
Optionally modify response
Response returned to caller
When an HTTP request starts, the interceptor catches it, modifies it (like adding headers), then sends it on. The response can also be caught and changed before reaching the caller.
Execution Sample
Angular
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    const cloned = req.clone({ setHeaders: { Authorization: 'Bearer token' } });
    return next.handle(cloned);
  }
}
This interceptor adds an Authorization header to every outgoing HTTP request.
Execution Table
StepActionRequest Headers BeforeRequest Headers AfterNext CalledNotes
1HTTP request starts{}{}NoInitial request has no headers
2Interceptor intercept() called{}{}NoInterceptor receives original request
3Clone request adding Authorization header{}{Authorization: 'Bearer token'}NoRequest cloned with new header
4Pass cloned request to next(){Authorization: 'Bearer token'}{Authorization: 'Bearer token'}YesModified request sent forward
5Server receives request{Authorization: 'Bearer token'}{Authorization: 'Bearer token'}YesServer sees added header
6Server sends responseN/AN/AYesResponse sent back
7Interceptor receives responseN/AN/AYesCan modify response if needed
8Response returned to callerN/AN/AYesCaller gets final response
💡 Request sent with added Authorization header; response returned to caller.
Variable Tracker
VariableStartAfter Step 3Final
req.headers{}{}{}
clonedundefinedHttpRequest with Authorization headerHttpRequest with Authorization header
Key Moments - 3 Insights
Why do we clone the request instead of modifying it directly?
HttpRequest objects are immutable in Angular. The interceptor must clone and modify the clone, as shown in step 3 of the execution_table.
When does the interceptor actually send the modified request?
After cloning and modifying the request, the interceptor calls next.handle(cloned) to forward it, as shown in step 4.
Can the interceptor modify the response too?
Yes, the interceptor can catch and modify the response after the server sends it, as shown in steps 7 and 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what headers does the request have after step 3?
A{Authorization: 'Bearer token'}
B{}
C{Content-Type: 'application/json'}
DNo headers
💡 Hint
Check the 'Request Headers After' column at step 3 in the execution_table.
At which step does the interceptor forward the modified request to the next handler?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for when 'Next Called' changes to 'Yes' in the execution_table.
If we did not clone the request before modifying, what would happen?
AThe server would reject the request.
BThe request would be modified successfully.
CAngular would throw an error because HttpRequest is immutable.
DThe interceptor would skip sending the request.
💡 Hint
Refer to the key_moments about immutability and cloning.
Concept Snapshot
Angular HTTP Interceptors modify requests/responses.
Use intercept() method to catch requests.
Clone request to add headers (immutable objects).
Call next.handle() with modified request.
Can also modify responses before returning.
Useful for auth tokens, logging, error handling.
Full Transcript
In Angular, interceptors let you catch HTTP requests before they go out and responses when they come back. The intercept() method receives the request, which is immutable, so you clone it to add or change headers like Authorization tokens. Then you pass the cloned request to next.handle() to continue the HTTP flow. The server receives the modified request. When the response comes back, the interceptor can also catch and modify it before it reaches your code. This process helps add common headers or handle errors globally.