Skip to content

Loading…

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

Closed
wants to merge 2 commits into from

8 participants

@maxmart

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

@vojtajina

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

@vojtajina

keep two empty lines in between specs...

@vojtajina

space after key: 123

@vojtajina

LGTM only little bit of formatting...

Could you please change commit message according to our convention https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit to

feat($resource): allow defining headers per action

Closes #736

Thank you very much. We really appreciate that.

@vojtajina

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):
http://code.google.com/legal/individual-cla-v1.0.html

For corporations (print, sign and scan+email, fax or mail):
http://code.google.com/legal/corporate-cla-v1.0.html

@vojtajina

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

@maxmart

Like this? :-)

@stefan79

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

@maxmart

ping... ? :)

@stefan79
@jarretraim

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?

@NathanWalker

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.

@zergin

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

@mhevery
Angular member

sorry about the delay, working on merging it now

@mhevery
Angular member

MERGED

@mhevery mhevery closed this
@nateabele

Woohoo! Thanks @mhevery!

@jarretraim

Thanks, this is great.

@mgonto mgonto referenced this pull request in mgonto/restangular
Closed

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
View
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']).
$http({
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 = response.data;
View
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.