export class HeroService {
heroesUrl = 'api/heroes'; // URL to web api
+ httpOptions = {
+ headers: new HttpHeaders({ 'Content-Type': 'application/json' })
+ };
constructor(
private http: HttpClient,
);
}
+ /** POST: add a new hero to the server */
+ addHero(hero: Hero): Observable<Hero> {
+ return this.http.post<Hero>(this.heroesUrl, hero, this.httpOptions).pipe(
+ tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
+ catchError(this.handleError<Hero>('addHero'))
+ );
+ }
+
/** GET hero by id. Will 404 if id not found */
getHero(id: number): Observable<Hero> {
this.log(`requested hero id=${id}`);
);
}
+ /** PUT: update the hero on the server */
+ updateHero(hero: Hero): Observable<any> {
+ return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
+ tap(_ => this.log(`updated hero id=${hero.id}`)),
+ catchError(this.handleError<any>('updateHero'))
+ );
+ }
+
/**
* Handle Http operation that failed.
* Let the app continue.