Arvind Chourasiya
Arvind Chourasiya

Reputation: 17472

Angular Rest API call returning 401 Unauthorized

My Angular project throwing error 401 Unauthorized when I am calling Rest Api's

"Http failure response for https://url/products/search: 401 Unauthorized"

This is my code

this.httpOptions = {
    headers: new HttpHeaders(
        { 
          'Content-Type': 'application/json; charset=utf-8',
          'org-code-rc' : 'VWiHL8RFR8fRGNjfZI=',
          'Authorization': `Basic` + btoa('myUsername:myPass'),
        }
    )
}

And getting result here

public getProducts() {

    return this._http.get(this.baseUrl, {
        headers: this.httpOptions
    });
}

How can I solve this issue?

Upvotes: 0

Views: 3759

Answers (2)

Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

You're already setting the headers so you need to change your code too:

public getProducts() {
     return this._http.get(this.baseUrl, this.httpOptions);
}

Because otherwise you will have the headers in the headers params and it won't work

Upvotes: 3

mbojko
mbojko

Reputation: 14699

For one, there's a space missing between Basic and the credentials:

  'Authorization': `Basic` + btoa('myUsername:myPass'),

Upvotes: 1

Related Questions