Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Angular 16 Reusable http service
http should not be called directly from a component. If the implementation changes in a future upgrade to Angular (it will), it should be wrapped in a custom service you created so that you can change the code in one place in your application.

Here is an example of an Angular16 GET service call. Adapt as needed for things like tracking while it's running, error handling, etc.

httpGet(url:string):Observable<any>{
// track is running here
return this.http.get(url)
.pipe(
finalize(()=>{
// shared actions on complete
}),
catchError(e => {
// actions on error, such as a notification or log
return throwError(()=> e);
}))
}


Here is an example of an Angular16 POST service call. Adapt as needed for things like tracking while it's running, error handling, etc. Here I show any returned, but for POST I prefer/recommend you always return the same object type with things like Success,ErrorMessage,Result,Message,etc.

httpPost(url:string, data:any):Observable<any>{
// track is running here
return http.post<any>(url,data)
.pipe(
.map(ret => {
// shared actions on complete
return ret;
}),
catchError(e => {
// actions on error, such as a notification or log, is running
return throwError(()=> e);
}));
}

Created By: amos 1/11/2024 9:42:09 AM
Updated: 1/11/2024 9:45:49 AM


 Comments:
 > Guest 1/24/2024 12:49:48 PM
Looks good
 > Guest 1/24/2024 12:52:35 PM
Test3