HTTP Interceptors — Angular Technical Interview Questions & Answers

HTTP Interceptors

HTTP Interceptors

  • An interceptor sits on every HTTP call. Add an Authorization header. Log errors. Retry once. Provide it with withInterceptors or HTTP_INTERCEPTORS.
  • Board: clone the request, set Bearer token from auth, pass to next. If 401, navigate to login. Trap: intercepting and forgetting to call next.handle — the request never leaves.

Technical Interview Tip: Add token + 401 → login. Must call next.

Code Examples for HTTP Interceptors

HTTP Interceptors — working example

import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  template: `
    <h1>{{ title }}</h1>
    <button (click)="toggle()">Toggle</button>
    <p *ngIf="open">Panel open</p>
  `,
})
export class AppComponent {
  title = "Angular practice";
  open = true;
  toggle() {
    this.open = !this.open;
  }
}

HTTP Interceptors: toggle open. Say whether the panel is on screen after one click.

How to Explain HTTP Interceptors to an Interviewer

What is an HTTP interceptor for?

Auth header Shared error handling