python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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
};
//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);
    }
}



ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions