AngularJS: Setting correct 'Referer' header for $http

John picture John · Mar 24, 2014 · Viewed 7.8k times · Source

It looks like when you send off a request using the $http service, the referer header is set to the base url without the angular $location information.

How do you go about appending the $location.path() to that header for all requests?

Our API calls will log that referer header when an error occurs; however, it would be very helpful if we could store the user's actual location ("stackoverflow.com/#/question/1234" opposed to just "stackoverflow.com/")

Answer

John picture John · Mar 24, 2014

I ended up just doing something like this:

$httpProvider.interceptors.push(function ($location) {
    return {
        request: function (config) {
            config.headers["RefererFullUrl"] = $location.absUrl();
            return config;
        }
    };
});

It doesn't look like the browsers are too happy if you try to change the 'referer' url, so I'm just naming it something else, and looking for that header specifically on the server