3

I am trying to add Authorization header to all my requests and am facing some issues. Even though I am adding the headers, the headers are not being used when network call is made.

Below is my interceptors code:

 const user: string = localStorage.getItem('user');
const token: string = localStorage.getItem('token');

const authReq = request.clone({
  headers: request.headers.set('Authorization', user + ',' + token)
});
return next.handle(authReq);

I have taken this code from angular documentation, I am not sure what I am missing here.

I've tried the following code as well, but no luck:

request = request.clone({
        setHeaders: {
          Authorization: `something`
        }
      });
1
  • 1
    change .set() to .append() it should work Commented Feb 22, 2019 at 5:30

2 Answers 2

3

@Logan give a try to this ,

user is not required along with your token unless your implementation requires it.

const user: string = localStorage.getItem('user');
const token: string = localStorage.getItem('token');

const  clonedRequest = req.clone({
   headers: new HttpHeaders({
         Authorization: token,
         "Content-Type": "application/json"
       })
    });

return next.handle(clonedRequest);
Sign up to request clarification or add additional context in comments.

1 Comment

glad to help you.. :)
1

Use for example:

let headers = new HttpHeaders({
    'Authorization': `Bearer ${token}`,
    'another', 'somedata'
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.