0
0
Angularframework~5 mins

Interceptors for request modification in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an HTTP interceptor in Angular?
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
What does the intercept method receive as parameters?
AHttpRequest and HttpHandler
BHttpResponse and HttpClient
CHttpEvent and HttpHeaders
DHttpClient and HttpInterceptor
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
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
What type does the intercept method return?
Avoid
BPromise<HttpResponse>
CHttpRequest
DObservable<HttpEvent<any>>
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.