Sagar Aher
Sagar Aher

Reputation: 81

httpheaders not set in angular 9 get?

In Angular 9

const headers = new HttpHeaders()
.set('Content-Type', 'application/json; charset=utf-8')
.set('Authorization', this.getToken());   
return this.httpClient.get(url,{headers:headers}).pipe(catchError(this.handleError));

can't connect httpheaders. headers not attached with get post put delete call why ?

Upvotes: 1

Views: 1711

Answers (2)

Mukund Sonaiya
Mukund Sonaiya

Reputation: 461

Here is the correct syntax :

const httpOptions = new HttpHeaders({
  'Content-Type': 'application/json',
  'Authorization': this.getToken()
});
this.httpClient.get(url, httpOptions).pipe(catchError(this.handleError));

Upvotes: 2

Aditya Vashishtha
Aditya Vashishtha

Reputation: 189

You can try this:-

let headers = new HttpHeaders()
headers=headers.set('Content-Type', 'application/json; charset=utf-8')
headers=headers.set('Authorization', this.getToken());   
return this.httpClient.get(url,{headers:headers}).pipe(catchError(this.handleError));

Since httpheader and httpparams are kinda immutable in nature so assiging it to the returned object should work.

Upvotes: -1

Related Questions