AngularJS HTTP Interceptor to set headers
Posted on 2015-03-28
Our new system is built in a distributed way. We have a bunch of services that have a clear responsibility and they are accessible over REST. Here’s how we handle a few headers for calling them in AngularJS.
The simplest use-case for this is internationalisation. Our app will need to be available in both French and English. Therefore, we are using HTTP’s
The REST API checks for this header and returns any localisable data in that language as well as error messages or any other resource that can be translated.
To do this globally in our AngularJS application, we used an HTTP interceptor. Here’s the gist :
This is also used by our authentication library to set the
Authentication header for obvious purposes.
This means that any request made in our app via the
$http provider will have those headers.