HttpClient

What is the HttpClient?

  • Provides API for network communication

  • Supports making HTTP request using (GET, POST, PUT, DELETE, etc verbs)

  • Handles headers for requests and responses

  • Built on RxJs Observables for asynchronous operations

Import HttpClient

core.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';

import { SharedModule } from '../shared/shared.module';
import { UserService } from './user.service';

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    SharedModule
  ],
  declarations: [],
  providers: [UserService]
})
export class CoreModule {}

HttpClient Example

HttpClient Service

Demo API

Observables

  • Angular uses the RxJS library to handle async operations

  • Observables provide a way to consume async data/events streams

    • Observables can be subscribed to and get data when the source emits a new value

  • Observables can be used to transform data

Implementing a Service in a Component

Complete Service

Summary

  • What is the HttpClient

  • Importing HttpClientModule

  • Making http calls

  • Observables introduction

Last updated