Q1. How do you handle HTTP responses?
Subscribe to the observable returned by HttpClient. The next handler receives the response body (by default). You can also get the full response using { observe: ''response'' } option to get status and headers.
Q2. How do you handle errors in HTTP requests?
Use the error callback in subscribe, or catchError operator from RxJS. Example:
this.http.get(''/api/users'').pipe(
catchError(this.handleError)
).subscribe(
data => this.handleData(data),
error => this.handleError(error)
);
Q3. What is the error object structure?
The error object passed to catchError or error callback contains details: error (the error object or body), status (HTTP status code), statusText, headers, etc. For network errors, error is an ErrorEvent. You can use this to provide user-friendly messages.
Q4. How do you retry failed requests?
Use the retry operator from RxJS. Example:
this.http.get(''/api/users'').pipe(
retry(3),
catchError(this.handleError)
).subscribe();
This retries up to 3 times on error.Q5. How do you show loading indicators?
Use a boolean flag. Set loading = true before request, false in finalize operator or in success/error handlers. Example:
this.loading = true;
this.http.get(''/api/users'').pipe(
finalize(() => this.loading = false)
).subscribe();
