0
votes

I am currently getting a 401 unauthorized error in the console for trying to access an api endpoint i built, which is propected. I noticed that I did not add the bearer token in the header. I noticed this and added this in however I am still getting the 401 error, what am i doing wrong? I know the token is valid because i am testing on Postman and i get 200 response code on Postman but 401 on angular.

here is my code:

let token = localstorage.get('token');

  getCategories() : Observable<any> {
    let token = localStorage.getItem("token");

    const headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.set('Authorization', `Bearer ${token}`);

    
    return this.http.get(this.CategoriesUrl, { headers: headers} )
  }