Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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 ?

question from:https://stackoverflow.com/questions/65881193/cant-access-api-using-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'
  })
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...