How can we pass the public and private key of basic auth in $http.put?
2 Answers
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.
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
fahad
How to pass basic authentication and its keys like public and private key in angular controller?
Wcan
.controller('Controller Name', ['$http', function($http) { $http.defaults.headers.common['Authorization'] = 'Basic ' + login + ':' + password); }]);
Wcan
Also you can make a factory out of it and then use the config within the factory to send the headers