1

My API uses the Accept-Language header to get it's current language which returns the json translated. Never mind that.

How can i change the header with angularJS/Javasctipt. I tried this:

  $http.defaults.headers.post["Accept-Language"] = "bs-Latn-BA";

but it doesn't seem to work, are there any other alternatives?

2 Answers 2

4

The default headers sent for every single request live in the $httpProvider.defaults.headers.common object.

You can change or augment these headers using the .config() function for every request, like so:

angular.module('myApp', [])
   .config(function($httpProvider) {
      $httpProvider.defaults.headers
        .common['Accept-Language'] = 'bs-Latn-BA';
});

We can also manipulate these defaults at run time using the defaults property of the $http object. For instance, to add a property for dynamic headers, we can set the header property like so:

$http.defaults
   .common['Accept-Language'] = "bs-Latn-BA";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, got it ^^ this way i can add any custom header?
2

AngularJS you can set common headers by using $httpProvider. Angularjs

Example:

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

    app.config(["$httpProvider", function($httpProvider) {
        // set Accept-Language header on all requests to
        $httpProvider.defaults.headers.common["Accept-Language"] = "bs-Latn-BA";
    }]);

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.