Search code examples
angularhttp-headersangular-httpclient

How do I pass header data with Angular 5?


This worked in Angular 4. What do I change for it to work in angular 5?

 getGreeting(): Observable<string> {
    let headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });

  //cant find requestoptions
  let options = new RequestOptions({ headers: headers });

    return this.http.
        get(Constant.ApiRoot + Constant.GreetingService, options).
        map((response: Response) => response.text());
}

Solution

  • The headers can be passed as HttpHeaders or Plain JSON object but it should be part of HttpOptions. You can find more about it on Angular's official documentation at https://angular.io/guide/http#adding-headers

    const httpOptions = {
          headers: new HttpHeaders({      
            'Authorization': Bearer ' + this.authenticationService.token 
          })
        };
    this.httpClientObj.get('url',httpOptions);