0

When I use $resource for a REST login, the transformRequest doesn't add the Authorization header as intended. Using a $.ajax call it does work as intended. So using:

    $scope.login2 = function() {
    function setHeader(xhr){xhr.setRequestHeader("Authorization", "Basic " + btoa($scope.gebruikersnaam + ':' + $scope.wachtwoord))}
    $.ajax({type: "POST",  url: "http://localhost:8000/authview/",  beforeSend: setHeader}).
        fail(function(resp){
            console.log('bad credentials.')
        }).
        done(function(resp){
            console.log('welcome ' + resp.email)
        })
}

I get the authorization header added to the request:

Origin: http://localhost
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36   (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Authorization: Basic YWRtaW46cGFzc3dvcmQ=

But when doing:

    $scope.login = function() {
  api.auth.login($scope.getCredentials()).
             $promise.
                 then(function(data){
                     // on good username and password
                     $scope.gebruikersnaam = data.username;
                 }).
                 catch(function(data){
                     // on incorrect username and password
                     alert(data.data.detail);
                 });      
};

where "api.auth.login" is defined like:

kmregistratieApp.factory('api', function($resource){
    function add_auth_header(data, headersGetter){
        var headers = headersGetter();
        headers['Authorization'] = ('Basic ' + btoa(data.username + ':' + data.password));
    }
    return {
        auth: $resource('http://localhost:8000/authview/', {}, {
            login: {method: 'POST', transformRequest: add_auth_header},
            logout: {method: 'DELETE'}
        }),
        users: $resource('http://localhost:8000/authview/', {}, {
            create: {method: 'POST'}
        })
    };
});

After "headers['Authorization'] = ('Basic ' + ..." (when debugging) I can see it sitting in headersGetter:

headers: Object
Authorization: "Basic YWRtaW46cGFzc3dvcmQ="
accept: "application/json, text/plain, */*"
content-type: "application/json;charset=utf-8"

But it doesn't turn up in the Network tab when inspecting the headers. So my question is why doesn't the $resource way of working not add the Authorization header?

0

1 Answer 1

2

TransformRequest is not meant to be used to modify headers. See AngularJS changelog. Scroll a bit downwards and you will see this:

transformRequest functions can no longer modify request headers.

HTTP headers can only be specified when using $http. Example:

$http.post('/someUrl', data, { headers: { 'Authorization': 'Basic'+key } });
Sign up to request clarification or add additional context in comments.

1 Comment

As far as I can see this change is for $http. The angular docs for $resource link clearly state: The transform function takes the http request body and headers... But if this change has an effect on $resource as well then this would explain the behaviour I guess (and perhaps the docs should change for it)

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.