alexsc alexsc - 4 months ago 31x
AngularJS Question

AngularJS $http custom header for all requests

I was wondering if there is any way to configure all $http requests header with adding custom info. Something like config :

var config = {headers: {
'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
'Accept': 'application/json;odata=verbose'

But for all $http calls I will make in different services. I'm sure there is a solution :D.Thanks


You can create a $http interceptor to extend your header:

myapp.factory('httpRequestInterceptor', function () {
  return {
    request: function (config) {

      config.headers['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';
      config.headers['Accept'] = 'application/json;odata=verbose';

      return config;

myapp.config(function ($httpProvider) {