Last Updated: February 25, 2016
·
796
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#angularjs
Did you know?

AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.

Security insights from Cside