DEV Community

Cover image for How to get token passed in header?
Akhil Ashok
Akhil Ashok

Posted on

How to get token passed in header?

how to get the header value in angular passed in header,i need to store the jwt token in localstorage and pass it into all api's

Top comments (3)

Collapse
 
sylvainmetayer profile image
Sylvain METAYER

See this SO answer, header can be accessed from the response class : stackoverflow.com/a/44292610

http.get('/path/to/resource')
  .subscribe((res:Response) => {
    console.log(res.headers);
    // you can assign the value to any variable here
  });
Collapse
 
dreamcatcher8055 profile image
Akhil Ashok

sorry bro i didnt get the answer in this way

Collapse
 
joseluisrnp profile image
José Luis Recio

How to send JWT in every HTTP request in Angular

  • Install the angular-jwt library using NPM: npm install @auth0/angular-jwt. Alternatively, create a HTTP interceptor yourself.
  • In your AppModule, you need to provide the way for the interceptor to retrieve the JWT. This should enable the HttpClient to get the JWT and include it in every request. Usually, you store the token either in a cookie (preferred) or in the browser storage.
  • Import the HttpClientModule in your module. This is required in order to inject the HttpClient service in a component/service/pipe/directive.
  • Use the HttpClient to send HTTP requests which will now include the JWT automatically.

Source: itnext.io/how-to-send-jwt-automati...