Http Get 401 (Unauthorized)


#1

Hi,

I’m calling a service using a token

Failed to load resource: the server responded with a status of 401 (Unauthorized)
Http failure response for http://localhost:65291/api/post: 401 Unauthorized

The same call works in Postman with Headers;

Content-Type: application/json
Authorization: Bearer token

The function in ionic is

getPosts() {
    var header = new HttpHeaders({ "Content-Type": "application/json" });
    header.append("Authorization", "Bearer " + this.token);
    console.log("Bearer " + this.token);
    return new Promise(resolve => {
      console.log(this.apiUrl + '/post');
      this.http.get(this.apiUrl + '/post', { headers: header}).subscribe((data: Post[]) => {
        resolve(data);
      }, err => {
        console.log(err);
      });
    });
  }

Added a log for the token to be sure that is adding it to the header correctly (the token is fine).
The apiUrl has value http://localhost:65291/api.

What is wrong here? Cors is enabled… Postman works ok…

Thanks