user781486 user781486 - 1 year ago 161
AngularJS Question

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 =;

I would like to add HTTP basic authentication to the HTTP GET. The username is
and the password is
. How can this be done?

Answer Source

Because in basic authentication, the username and password are decode by base64. You need to find a library to do this work for comfortable first.

After that, load base64 into your app and config headers.

    .module('myApp', ['base64'])
    .config(function($httpProvider, $base64) {
        var auth = $base64.encode("foo:bar");
        $httpProvider.defaults.headers.common['Authorization'] = 'Basic ' + auth;

You can also provide the authentication header to get function seprately if you like.

var auth = $base64.encode("foo:bar"), 
    headers = {"Authorization": "Basic " + auth};
$http.get(url, {headers: headers}).then(function (response) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download