1

I would like to capture a token that I set in the http header inside my Angular application.

This is how I am serving my Angular application :

var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
var router = express.Router();
var utils = require('./utils);

console.log('——————————- Run on port '+ port);

/****************************** Router ***************************/
router.get('*', function(req, res){
    token = utils.generateToken(); 
    res.set('X-Auth', token).sendFile('index.html', { root: __dirname + '/public/app' });
});

This token that I am setting in the header is what I would like to capture inside my Angular application. I am kind of lost on where and how to do that ? I am using Angular 9. Any hints where to start ?

0

2 Answers 2

1

the http clent return JSON data if you want to read all respond body you need to set observe option to responsenow the result will be of type HttpResponse you can now read all respond header ,type, status

http
  .get<any>('url', {observe: 'response'})
  .subscribe((res : HttpResponse) => {
    console.log(rep.headers.get('X-Token'));
    localStorage.setItem('token',res.headers.get('X-Token'))
  });

check this 👉 http reading the full response

Sign up to request clarification or add additional context in comments.

Comments

0

You can get the headers from the Response Class like below:

http.get('/controller/action')
    .subscribe((response:Response) => {
       console.log(response.headers);
     });

1 Comment

this will work in old verion but now ,http return response.json() by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.