I have an existing angularjs code which makes a HTTP GET. Extracted below is some relevant code inside the controller.
.controller('imptViewCtrl', ['$scope', '$http',
function ($scope, $http, ) {
var url = $configuration.webroot + '/impt/list?list=testlist';
$http.get(url).then(function (response) {
tableData = response.data;
});
}]);
I would like to add HTTP basic authentication to the HTTP GET. The username is foo
and the password is bar
. How can this be done?
AngularJS Authentication ServiceThe Authentication Service is the interface between the angularjs app and the server side api, so http requests are managed here. It also contains a Base64 service for encoding the username and password to be used in the HTTP Authorization header for all requests after logging in.
To configure your web service client to use HTTP basic authentication, supply the HttpTransportProperties. Authenticator object in your client Java code, and specify a user name and a password. Set this configuration as an option of the web service client.
In angularjs get ($http. get()) service or method is used to get data from remote HTTP servers. In angularjs get is a one of the shortcut method in $http service. In other ways, we can say that $http. get service or method in $http service is used to get data from the given URI.
The Angular 9 basic authentication example app uses a fake backend by default so it can run in the browser without a real api, to switch to a real backend api you just have to remove or comment out the line below the comment // provider used to create fake backend located in the /src/app/app. module.
Rather than using a library to encode your auth by base 64, you could just use:
window.btoa("user:pass")
it's supported by most browsers.
https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With