custom header http request in angularjs

How to set authorization header on $http request in angularjs with MVC5?

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

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 ( !== undefined && !== null) {
                $scope.users =;

MVC5 part code sample as given below

public class Users_APIController : ApiController
    UsersRepository _repoUser = new UsersRepository();

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


Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
Post a Comment Powered by Blogger.
ASK Questions