When interacting with an Express.js API, I encountered a issue regarding the handling of auth tokens. The problem arose when sending the token in the request headers using Angular 4 compared to Postman. In Postman, setting the header named 'Authorization' worked perfectly. However, when trying to send it with Angular 4 like this:
const headers = new HttpHeaders()
.set('Content-Type', 'application/json')
.set('Authorization', `${token}`);
this.http.get<Response>(`${this.apiURL}dashboard/`, { headers: headers })
.subscribe(res => console.log(res));
Upon logging the headers object, everything appeared to be correctly set. But attempting to retrieve the token in my express route using:
const token = req.get('Authorization');
always resulted in 'undefined'. It raised the question: What is angular doing differently from postman? https://i.stack.imgur.com/SorQA.png
Here is the logged result when trying to extract the token: https://i.stack.imgur.com/9PriP.png
Postman continues to work flawlessly: https://i.stack.imgur.com/BR0Vf.png