1

How can we pass the public and private key of basic auth in $http.put?

2 Answers 2

1

You can create httpProvider interceptor:

angular.module('app').factory('apiInterceptor', function(token){
    return {
        request: function(req) {
            req.headers.Authorization = token.get();
            return req;
        }
    }).config(function($httpProvider){
        $httpProvider.interceptors.push('apiInterceptor');
    }).provider('token', function() {
         var token = '';
         return {
             get: function() {
                 return token;
             },
             set: function(t) {
                 token = t;
             }
         }
    }).controller('myController', function(token) {
         token.set('your token');
    });

Something like this.

Sign up to request clarification or add additional context in comments.

2 Comments

How to pass basic authentication and its keys like public and private key in angular controller?
You can create angular provider which will hold your informations.
0

you will have to do it in the configuration (you app.js file) or you could do that in your controller as well

app.run(['$http', function($http) {
    $http.defaults.headers.common['Authorization'] = /* ... */;
}]);

app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common['Authorization'] = /* ... */;
}])

3 Comments

How to pass basic authentication and its keys like public and private key in angular controller?
.controller('Controller Name', ['$http', function($http) { $http.defaults.headers.common['Authorization'] = 'Basic ' + login + ':' + password); }]);
Also you can make a factory out of it and then use the config within the factory to send the headers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.