Q1. What is HttpClient in Angular?
HttpClient is Angular''s built-in module for making HTTP requests to backend servers. It provides a simplified API for common operations like GET, POST, PUT, DELETE. It returns observables, supports request/response interception, and includes features like typed responses, progress events, and testing utilities.
Q2. How do you set up HttpClient?
Import HttpClientModule in your AppModule or feature module. Example:
import { HttpClientModule } from ''@angular/common/http'';
@NgModule({ imports: [HttpClientModule] })
Then inject HttpClient into services: constructor(private http: HttpClient) {}.Q3. What types of HTTP requests can HttpClient make?
HttpClient supports all standard HTTP methods: GET (get data), POST (create), PUT (update), PATCH (partial update), DELETE (remove), HEAD, and OPTIONS. Each returns an Observable that emits the response when complete.
Q4. How does HttpClient handle JSON by default?
HttpClient automatically parses JSON responses into JavaScript objects. It expects JSON by default and sets Content-Type accordingly. If you request a non-JSON response, you need to specify the responseType. For example, responseType: ''text'' for plain text.
Q5. What are the advantages of using HttpClient over the old Http module?
HttpClient is more modern, uses observables, has automatic JSON parsing, supports typed responses, provides request/response interceptors, and includes progress events. It''s also easier to test with HttpTestingController.
