Almin Islamovic Almin Islamovic - 6 months ago 145
AngularJS Question

Can i change Accept-Language Request Header with AngularJS

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!

Answer

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