0

I am trying to set some customer headers in a service factory. Here is the code:

angular.module('clinicalApp').factory('encounterService', function ($resource) {
  var EncounterService = $resource('http://localhost:port/v1/providers/:providerId', {providerId:'@id', port: ':8280'}, {
    search: {
      method: 'GET',
      isArray: true,
      headers: {
        'RemoteUser': 'billybob'
      }
    }
  });
  return EncounterService;
});

Here is the code that calls the service.

angular.module('clinicalApp').controller('MainCtrl', function ($scope, encounterService) {
  encounterService.search({
    limit: 2000,
    organizationId : '11110000'
  });
});

When I use this resource and everything works fine, but the header is not set on the ajax call, so I get a 401 in return. What else do I have to do to set the headers? Thanks for the help.

3 Answers 3

2

I am sure the other answers I received work, but I did not want to use $http, I wanted to use $resource. To use resource with custom headers, I had to upgrade my Angular version. I did not look into the source code to find the reason why, and I don't know what version this functionality changed. Right now I am using v1.2.0-rc.2 and everything just worked. It took a few changed in the app config, namely I had to name ngRoute as a dependency to make the version work, but then I was able to use $resource like we are supposed to do.

Sign up to request clarification or add additional context in comments.

Comments

1

Don't use run(), use config() with $httpProvider:

app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common = {
        'RemoteUser': 'billybob'
    };
}])

Comments

0
clinicalApp.run(function($http) {
    $http.defaults.headers.common["Content-Type"] = "application/json, charset=UTF-8";
});

2 Comments

Where is this added at?
@jhamm I modified my answer.

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.