Custom Headers for $resource - AngularJs

71 views Asked by At

Hi I am new to Angular JS, I am trying to put custom header in my $resource method, but I am not sure how would I do that Following is my code :

function userFactory($resource, API) {
    return $resource(API + '/users/:cmd', {}, {}, {
            login: {method: 'GET', params: {cmd: 'current'}, headers: headerParam},
            get: {method: 'GET'},
            create: {method: 'POST'}
        }
    );

I would like to add a custom header 'headerParam' based on which user has logged in since the header would contain username and password specific details.

1

There are 1 answers

0
Chandermani On

As I look at the header documentation in $http.config. This is map of string\ function, which implies you can call a function to set specific header

Something like

headers:{credentials: getUserCredentials}

And in the factory you can create the above method.

var getUserCredentials = function(config) {
}

I have not tried, see if it helps.