97hnna
Last Updated: February 25, 2016
·
471
· dankapusta

Set your default headers ONCE

In AngularJs 1.1.4 (the only version I tested this in), if you set $http.defaults.headers.post twice, the second configuration will overwrite the first, so set an object of key:value pairs once instead of doing them one at a time.

Do this.

$http.defaults.headers.post  = {
  'My-Custom-Header': obj.val,
  'X-Forwarded-User': obj.userid
};

Not this.

$http.defaults.headers.post  = {
  'My-Custom-Header': obj.val
};
$http.defaults.headers.post  = {
  'X-Forwarded-User': obj.userid
};

In this second example, your POSTs will only have the X-Forwarded-User header set, My-Custom-Header will NOT be there.