0
0
Angularframework~20 mins

Interceptors for request modification in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
  }
}
ABlocks all outgoing HTTP requests and returns an error.
BRemoves any Authorization header from outgoing HTTP requests.
CModifies the request URL to add a query parameter 'token=token123'.
DAdds an Authorization header with value 'Bearer token123' to every outgoing HTTP request.
Attempts:
2 left
💡 Hint
Look at the clone method and what headers it sets.
📝 Syntax
intermediate
2: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?
Aconst clonedReq = req.clone({ params: req.params.append('lang', 'en') });
Bconst clonedReq = req.clone({ setParams: { lang: 'en' } });
Cconst clonedReq = req.clone({ url: req.url + '?lang=en' });
Dconst clonedReq = req.clone({ queryParams: { lang: 'en' } });
Attempts:
2 left
💡 Hint
Check the HttpRequest clone method options for adding query parameters.
🔧 Debug
advanced
2: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);
  }
}
AThe interceptor forgot to call next.handle(req).
BHttpRequest headers are immutable; set() returns a new headers object but does not modify the original.
CThe header name 'X-Custom-Header' is invalid and causes an error.
DThe interceptor must use req.clone() to modify the body, not headers.
Attempts:
2 left
💡 Hint
Think about immutability of HttpRequest and its headers.
state_output
advanced
2: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);
}
A'Token2', because the second interceptor overwrites the header set by the first.
B'Token1', because the first interceptor sets it and the second does not change it.
C'Token1, Token2', both headers are combined.
DNo Authorization header is sent because of conflict.
Attempts:
2 left
💡 Hint
Later interceptors can overwrite headers set by earlier ones.
🧠 Conceptual
expert
2:00remaining
Which statement about Angular HTTP interceptors is true?
Choose the correct statement about Angular HTTP interceptors and request modification.
AInterceptors can only modify the response, not the request.
BInterceptors can directly modify the HttpRequest object without cloning it.
CInterceptors can modify requests but must always clone the HttpRequest object because it is immutable.
DInterceptors are only called once per application lifetime.
Attempts:
2 left
💡 Hint
Think about how Angular handles HttpRequest objects internally.