7

Is there a way to change or edit the accept Language header that i send to my API ? is there a way in javascript Jquery or Angular? i don't want to send the default one, but the one my Cookie has!

Almin Islamovic
  • 278
  • 5
  • 13
  • This has been answer [here][1], please check its not a duplicate. [1]: http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – daxeh May 13 '15 at 13:27

2 Answers2

5

In AngularJS you can set common headers by using $httpProvider and you can get the cookies by using $cookies service.

For example:

var app = angular.module("app", []);

app.config(["$httpProvider", "$cookies", function($httpProvider, $cookies) {
    // set Accept-Language header on all requests to
    // value of AcceptLanguageCookie cookie
    $httpProvider.defaults.headers.common["Accept-Language"] = $cookies.get("AcceptLanguageCookie");

    // or set headers on GET requests only
    if (!($httpProvider.defaults.headers).get) {
        ($httpProvider.defaults.headers).get = {};
    }
    $httpProvider.defaults.headers.get["Test-Header"] = "value";
}]);
gaiazov
  • 1,908
  • 14
  • 26
2

To reference the cookie which contains your specific "Accept-Language" value you will want to do this at run time. The reason is that the app config only accepts Providers and the reference to Services is not recommended.

To do this at run time you will want to make use of the $http and $cookies services

Here's an example:

var app = angular.module("app", []);

app.run(["$http", "$cookies", function($http, $cookies){
  // set Accept-Language header on all requests
  $http.defaults.headers.common["Accept-Language"] = $cookies.get("LocaleCookie");
}]);

To statically set the "Accept-Language" WITHOUT a cookie then you can use .config()

var app = angular.module("app", []);

app.config(["$httpProvider", function($httpProvider) {
  // set Accept-Language header on all requests
  $httpProvider.defaults.headers.common["Accept-Language"] = "fr";
}]);
Kevin B
  • 969
  • 9
  • 9