1

I have one GET endpoint.

It has HTTP Basic Authentication enabled. I want to create a GET request to the given end point.

https://example.com/api GET 

User Name :- admin

Password :- admin

My Code :-

$scope.listData = function() {
  $http.get('https://example.com/api').then(function(response) {
    $scope.items = response.data;
  });
}

What is the recommended way to pass the authentication?

Dinesh Ahuja
  • 925
  • 3
  • 15
  • 29
  • 2
    You must pass the `Authorization` request header such that `Authorization: Basic [base64_encode('username' + ':' + 'password')]` – marekful Jul 23 '18 at 11:51
  • 1
    duplicate of https://stackoverflow.com/questions/11876777/set-http-header-for-one-request – VinuBibin Jul 23 '18 at 12:07

2 Answers2

0

Second argument for the GET is the header part. For ex.

$http.get('www.google.com/someapi', {
    headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}).then()..;
Bert Maurau
  • 979
  • 5
  • 21
0

the recommended method is http-interceptors. what interceptor do, you can assume its a hook which call on every api request and response, once you write it, it will automatically add token in every api request. below is the url you read the article.

Angular Authentication: Using the Http Client and Http Interceptors

M Usman Nadeem
  • 415
  • 2
  • 13