HTTP Client — Angular Technical Interview Questions & Answers

HTTP Client

HTTP Client

  • HttpClient does AJAX. this.http.get<Student[]>('/api/marks').subscribe(rows => this.students = rows). provideHttpClient() in app config (or HttpClientModule on older apps).
  • Always handle error: catchError → user message. Don’t leave subscribe(err) empty. Unsubscribe or use async pipe / takeUntilDestroyed so you don’t setState after destroy.
  • Trap: putting the URL string in ten components. Keep it in the service. Another trap: JSON.parse on an already-parsed body.

Technical Interview Tip: get in a service + error + no leak after destroy.

Code Examples for HTTP Client

HTTP Client — 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 Client: toggle open. Say whether the panel is on screen after one click.

How to Explain HTTP Client to an Interviewer

Where should HttpClient live?

A service, not the template

What if the user leaves the page mid-request?

Unsubscribe / takeUntilDestroyed