Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HTTP basic authentication to this HTTP GET in angularjs

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?

like image 472
guagay_wk Avatar asked Dec 22 '15 10:12

guagay_wk


People also ask

Can we use basic HTTP auth in Angularjs?

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.

How do I set basic authentication in HTTP?

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.

What is HTTP get in Angularjs?

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.

What is basic authentication How do you implement it using angular?

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.


1 Answers

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

like image 129
ryan.wise Avatar answered Oct 12 '22 15:10

ryan.wise