Angular › HTTP Client & Interceptors
How do you make a basic GET request to a server using Angular's HttpClient?
BeginnerAnswer
You inject the HttpClient service into your component or service, and call its get method with the target URL, which returns an observable that emits the response once the request completes successfully.
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
getUsers() {
return this.http.get<User[]>('/api/users');
}
// Elsewhere
this.userService.getUsers().subscribe((users) => console.log(users));Real-world example A user list component fetches all users from a backend API using HttpClient's get method, subscribing to the resulting observable to display the data once it successfully arrives.