Skip to content


Fix for issue #736: ngResource now accepts headers #965

wants to merge 2 commits into from

8 participants


With this fix it is possible to specify HTTP headers in the actions parameter of ngResource.
For example, to be able to send conditional PUT requests you could specify:

$resource('/CreditCard/123', {}, {
  add: {
    method: 'PUT',
    headers: {'If-None-Match': '*'}

As I understand it, this should resolve issue #736


please, revert this, to keep new line at the end of the file


keep two empty lines in between specs...


space after key: 123


LGTM only little bit of formatting...

Could you please change commit message according to our convention to

feat($resource): allow defining headers per action

Closes #736

Thank you very much. We really appreciate that.


Thanks for your contribution! In order for us to be able to accept it, we ask you to sign our CLA (contributor's license agreement).

CLA is important for us to be able to avoid legal troubles down the road.

For individuals (a simple click-through form):

For corporations (print, sign and scan+email, fax or mail):


Oops, I'm sorry Max, I can see you already did sign the CLA, so please ignore my previous msg...


Like this? :-)


Cool. Starting using angular today, found the problem with base authenticated rest resources this afternoon and found the patch this evening. Impressed.....


ping... ? :)


Any chance I can get an update on this one? I'm trying to integrate with a Rackspace ReST API and they auth using a custom X-Auth-Header. Is there a workaround without this patch? Or should I just patch my local copy?


I am also curious of the status on this pull request. Similar to above, trying to integrate with a RESTapi that uses Basic Authentication. Would really like to be able to set the 'Authorization' header with a $resource.


I'd also like to know the status of this issue. It's critical to be able to set authorization header (among others).

Angular member

sorry about the delay, working on merging it now

Angular member


@mhevery mhevery closed this

Woohoo! Thanks @mhevery!


Thanks, this is great.

@mgonto mgonto referenced this pull request in mgonto/restangular

Headers not send #40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 17, 2012
  1. @maxmart
Commits on Jun 5, 2012
  1. @maxmart
Showing with 22 additions and 4 deletions.
  1. +6 −4 src/ngResource/resource.js
  2. +16 −0 test/ngResource/resourceSpec.js
10 src/ngResource/resource.js
@@ -36,8 +36,8 @@
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
* default set of resource actions. The declaration should be created in the following format:
- * {action1: {method:?, params:?, isArray:?},
- * action2: {method:?, params:?, isArray:?},
+ * {action1: {method:?, params:?, isArray:?, headers:?},
+ * action2: {method:?, params:?, isArray:?, headers:?},
* ...}
* Where:
@@ -49,6 +49,7 @@
* - `params` – {object=} – Optional set of pre-bound parameters for this action.
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
* `returns` section.
+ * - `headers` – {object=} – Optional HTTP headers to send
* @returns {Object} A resource "class" object with methods for the default set of resource actions
* optionally extended with custom `actions`. The default set contains these actions:
@@ -130,7 +131,7 @@
* The object returned from this function execution is a resource "class" which has "static" method
* for each action in the definition.
- * Calling these methods invoke `$http` on the `url` template with the given `method` and `params`.
+ * Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
* When the data is returned from the server then the object is an instance of the resource type and
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
* operations (create, read, update, delete) on server-side data.
@@ -362,7 +363,8 @@ angular.module('ngResource', ['ng']).
method: action.method,
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
- data: data
+ data: data,
+ headers: extend({}, action.headers || {})
}).then(function(response) {
var data =;
16 test/ngResource/resourceSpec.js
@@ -14,7 +14,14 @@ describe("resource", function() {
patch: {
method: 'PATCH'
+ },
+ conditionalPut: {
+ method: 'PUT',
+ headers: {
+ 'If-None-Match': '*'
+ }
callback = jasmine.createSpy();
@@ -146,6 +153,15 @@ describe("resource", function() {
+ it('should send correct headers', function() {
+ $httpBackend.expectPUT('/CreditCard/123', undefined, function(headers) {
+ return headers['If-None-Match'] == "*";
+ }).respond({id:123});
+ CreditCard.conditionalPut({id: {key:123}});
+ });
it("should read partial resource", function() {
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
var ccs = CreditCard.query();
Something went wrong with that request. Please try again.