0

I am consuming a REST api using Angular JS, using $resource module. I have to encode the URL using a private key and send the results of the encoding process on a header. I am trying to intercept the request and getting the requested URL, but I could not do that.

return $resource(url, {}, {
    get: { method: 'GET', headers: headers, transformRequest: function(data, headersGetter) {
        // Here "data" is undefined.  headersGetter() returns the headers. 
        // I need the URL here
    } 
});

Any help?

1 Answer 1

1

It sounds like what you want is an interceptor. Find the Interceptors section of the $http documentation. It would look something like this:

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    return {
        // optional method
        'request': function(config) {
            //modify headers in config based on url in config
            return config;
        },
    }
});

Then register the interceptor like this:

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('myHttpInterceptor');
}]);
Sign up to request clarification or add additional context in comments.

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.