How to set HTTP header for $http.GET request in
angularjs?
The AngularJs code sample as given below
//This is the global
var baseURL = "http://localhost:8080/";
var emailId =
window.utility.user.EmailID
var userID =
window.utility.user.UserID;
var userLocationID =
window.utility.user.UserLocationID;
var authInfo = {
Authorization: 'Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
UserID: userID,
UserLocationID: userLocationID
};
//END
var app =
angular.module('authApp',
[]);
//This is the angular
services
app.service("myAuthServices", function ($http) {
this.getUsers = function (url) {
return $http({
method: 'GET',
url: baseURL + url,
headers: authInfo
});
};
});
//This is the angular
controller.
app.controller('baseController', function ($scope, myAuthServices)
{
//This is default values.
$scope.users = null;
var apiURL = "api/Users_API/GetUsers/";
//Call get user services.
myAuthServices.getUsers(apiURL).then(function (resp) {
if (resp !== undefined
&& resp !== null) {
if (resp.data !==
undefined && resp.data !== null) {
$scope.users = resp.data;
}
}
});
});
MVC5 part code sample as given below
[RoutePrefix("api/Users_API")]
public class Users_APIController : ApiController
{
UsersRepository _repoUser = new UsersRepository();
[Route("GetUsers")]
public IHttpActionResult GetUsers()
{
long UserID = 0,
UserLocationID = 0; string EmailID = null;
if
(Request.Headers.Contains("Authorization"))
EmailID =
Request.Headers.GetValues("Authorization").First();
if
(Request.Headers.Contains("UserID"))
UserID = Convert.ToInt32(Request.Headers.GetValues("UserID").First());
if
(Request.Headers.Contains("UserLocationID"))
UserLocationID = Convert.ToInt32(Request.Headers.GetValues("UserLocationID").First());
var result =
_repoUser.GetCompUsers(EmailID, UserID, UserLocationID);
return Json(result);
}
}