Challenge - 5 Problems
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Angular HTTP interceptor do to outgoing requests?
Consider this Angular HTTP interceptor code snippet. What is the effect on the outgoing HTTP requests?
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 clonedReq = req.clone({ setHeaders: { Authorization: 'Bearer token123' } }); return next.handle(clonedReq); } }
Attempts:
2 left
💡 Hint
Look at the clone method and what headers it sets.
✗ Incorrect
The interceptor clones the original request and adds an Authorization header with the value 'Bearer token123'. This modified request is then passed on.
📝 Syntax
intermediate2:00remaining
Which option correctly modifies the request URL in an Angular interceptor?
You want to add a query parameter 'lang=en' to every outgoing HTTP request URL. Which interceptor code snippet correctly does this?
Attempts:
2 left
💡 Hint
Check the HttpRequest clone method options for adding query parameters.
✗ Incorrect
The correct way to add query parameters is to use the params property and append the new parameter.
🔧 Debug
advanced2:00remaining
Why does this interceptor fail to add the header?
This interceptor code is intended to add a custom header but it does not work. Why?
Angular
import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http'; @Injectable() export class CustomHeaderInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler) { req.headers.set('X-Custom-Header', 'value123'); return next.handle(req); } }
Attempts:
2 left
💡 Hint
Think about immutability of HttpRequest and its headers.
✗ Incorrect
HttpRequest and its headers are immutable. Calling set() on headers returns a new headers object but does not change the original. You must clone the request with the new headers.
❓ state_output
advanced2:00remaining
What is the output of this interceptor chain modifying headers?
Given two interceptors chained in this order, what is the final Authorization header value sent in the HTTP request?
Angular
/* Interceptor 1 */ intercept(req, next) { const cloned = req.clone({ setHeaders: { Authorization: 'Token1' } }); return next.handle(cloned); } /* Interceptor 2 */ intercept(req, next) { const cloned = req.clone({ setHeaders: { Authorization: 'Token2' } }); return next.handle(cloned); }
Attempts:
2 left
💡 Hint
Later interceptors can overwrite headers set by earlier ones.
✗ Incorrect
Each interceptor clones the request and sets the Authorization header. The last interceptor's value overwrites previous ones.
🧠 Conceptual
expert2:00remaining
Which statement about Angular HTTP interceptors is true?
Choose the correct statement about Angular HTTP interceptors and request modification.
Attempts:
2 left
💡 Hint
Think about how Angular handles HttpRequest objects internally.
✗ Incorrect
HttpRequest objects are immutable. Interceptors must clone them to modify headers, URL, or body.