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!

1

2 Answers 2

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";
}]);
Sign up to request clarification or add additional context in comments.

Comments

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";
}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.