I'm trying to get JSON from the server with login and password in the header.
There are no credentials in the request.
var app = angular.module("app", []);
app.factory('AuthService', function() {
var currentUser;
var login = '[email protected]';
var password = 'hello';
return {
//login: function() {
//},
//logout: function() {
//},
login: login,
password: password,
isLoggedIn: function() {
return currentUser != null;
},
currentUser: function() {
return currentUser;
}
};
});
app.run(['$http', 'AuthService', function($http, AuthService) {
/* Using btoa to do Base64 */
$http.defaults.headers.common['Authorization'] = 'Basic ' + btoa(AuthService.login + ':' + AuthService.password);
}]);
app.controller('LabController', function($scope, $http){
$scope.labs = $http.get('http://127.0.0.1:5000/api/v1.0/labs').error(function(data, status, headers, config) {
//console.log("Data");
console.log(data);
//console.log("Headers");
//console.log(headers());
});
window.labs = $scope.labs;
});
Here is what I get in the request header.
Host: 127.0.0.1:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://localhost:5000
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
Connection: keep-alive
Cache-Control: max-age=0
The Authentication Header (AH) protocol provides data origin authentication, data integrity, and replay protection. However, AH does not provide data confidentiality, which means that all of your data is sent in the clear.
The Authentication Header (abbreviated as AH) is a security mechanism that aims to help with authenticating the origins of packets of data that are transmitted under IP conditions (also known as the datagrams).
An authentication header is required for all calls to the REST endpoint. The Authorization field in the HTTP header is used to pass user credentials. When authentication fails, the error code 401 (Unauthorized) is returned with additional information in the WWW-Authenticate header of the response.
I would prefer to write my code this way..
//This is a web service service which includes get and post type calls and also adds the required authentication headers to all the requests
myApp.factory('webService', function ($http,loadingActivity) {
return{
postRequest: function (requestUrl, requestData,contentType,successCallbackFn,errorCallbackFn,showLoading) {
var httpRequest = {method: "POST", url: requestUrl, headers: {'userName':userName, 'password':password, 'Content-Type': contentType}, data: requestData };
$http(httpRequest).success(function (data, status) {
loadingActivity.hide();
successCallbackFn(data,status);
}).error(function (data, status) {
loadingActivity.hide();
errorCallbackFn(data,status);
});
},
getRequest:function(requestUrl, contentType, successCallbackFn, errorCallbackFn, showLoading){
var httpRequest = {method: "GET", url: requestUrl, headers: {'userName':userName, 'password':password, 'Content-Type': contentType}};
$http(httpRequest).success(function (data, status) {
loadingActivity.hide();
successCallbackFn(data,status);
}).error(function (data, status) {
loadingActivity.hide();
errorCallbackFn(data,status);
});
}
}
});
Here you can replace userName and password with your Authorization header. This code is well tested and I am using it in production environment. In your controller simpy inject webService and then you can call this this way..
webService.getRequest(preferences.loginURL+data,"application/json",function(data,status){
},function(data,status){
showAlert(messages.requestFailedError,"Login Failed!");
},true);
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