Search code examples
angularapiapi-authorization

can't access api using angular


I have an API, which I need to consume to create reports, However that API requires username and password to access it, by using curl command I can access the API

curl -X GET "https://SomeApiCall/api/v1/customers/" -H "accept:application/json; charset=UTF-8" -u "username:password"

but when I am trying to use same API using angular I am getting below error

key:"api.error.invalid_auth_schema"
message: "Invalid authentication scheme is used. Only Basic Authentication scheme is supported (login and password should be provided)"

Angular sample code:

 this.data = this.http.get("https://SomeApiCall/api/v1/customers/" , {
      headers: new HttpHeaders({
        'Authorization': 'Basic' + btoa('username:password'),
        'Content-Type':  'application/json'
      })
    });

Can someone explain, what mistake I am making ?


Solution

  • I think the problem is that you don't have a space after 'Basic'. Change to:

     this.data = this.http.get("https://SomeApiCall/api/v1/customers/" , {
      headers: new HttpHeaders({
        'Authorization': 'Basic ' + btoa('username:password'),
        'Content-Type':  'application/json'
      })
    });