liveBook · Manning

HTTP client

As explained in Pro Angular 16

HttpClient is an Angular service for making HTTP requests and receiving asynchronous responses.

HttpClient is an Angular service for making asynchronous HTTP requests and receiving responses as RxJS observables. Inject it into a data-source class, then call a method such as get or post for a RESTful web service. section 23.3.1 section 6.6

Choose the method that matches the HTTP operation:

  • get(url) retrieves data; post(url, body) submits an object; put(url, body) replaces data. section 23.3.1
  • patch(url, body) partially updates data; delete(url) removes it. section 23.3.1
  • head(url) requests headers without a response body; options(url) sends an OPTIONS request. section 23.3.1
  • request(method, url, options) supports a request with any HTTP verb. section 23.3.1

Typed product request

Inject HttpClient and return a typed observable: `constructor(private http: HttpClient) { }` followed by `getData(): Observable<Product[]> { return this.http.get<Product[]>(REST_URL); }`. The type parameter causes JSON data to be represented as Product[], while subscription triggers the request. section 23.3.1

Listing 23.5. The contents of the rest.datasource.ts file in the src/app/model folder
import { Injectable, Signal } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Product } from "./product.model";
import { Observable } from "rxjs";
 
export const REST_URL = `http://${location.hostname}:3500/products`;
 
@Injectable()
export class RestDataSource {
 
    constructor(private http: HttpClient) { }
 
    getData(): Observable<Product[]> {
        return this.http.get<Product[]>(REST_URL);
    }
}

Use the complete RestDataSource listing to see HttpClient injection, a REST endpoint constant, and a typed GET method together. section 23.3.1

Enable HttpClient through HttpClientModule, provide the data-source service, and adapt repositories to consume its observables or convert them to signals. Network delay, repeated subscriptions, cross-origin rules, and user-visible HTTP errors require explicit application handling. section 6.6.1 section 23.8

How Pro Angular 16 covers it

How deep it goes
Explains injection, typed JSON observables, HTTP verbs, subscription behavior, and integration, while deferring detailed request configuration.
Examples
Demonstrates a typed product GET request and a SportsStore data source that loads products and posts orders.
What it assumes
Assumes basic Angular services and REST concepts while teaching HttpClient injection, HTTP methods, observables, and module setup.
What you can do after
Build asynchronous REST data sources with typed responses and connect them to Angular repositories.
How it connects
Ties HTTP requests to RESTful web services, observables, signals, HttpClientModule, CORS, and HTTP error handling.

Also explained in

Compare how 4 books explain HTTP client →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.