Re: [angular.js] $resource and headers

7,956 views
Skip to first unread message

Pawel Kozlowski

unread,
Jul 23, 2012, 3:31:29 PM7/23/12
to ang...@googlegroups.com
Hi Michael!

The callback function you can pass to a resource's get function can
receive the same params as the $http
(http://docs.angularjs.org/api/ng.$http) get method, that is:
data – {string|Object} – The response body transformed with the
transform functions.
status – {number} – HTTP status code of the response.
headers – {function([headerName])} – Header getter function.
config – {Object} – The configuration object that was used to generate
the request.

Would be far, far easier to have a jsFiddle (even if broken!) but you
should be doing sth like:

Logins.get({[your query criteria here]}, function(data, status, headers){
//you've got access to headers here
});

In short a resource object is just a wrapper for the $http service.

Hope it clarifies things a bit but feel free to send a jsFiddle (once
again, even broken) so we can collaborate on a piece of code.

Cheers,
Pawel

On Mon, Jul 23, 2012 at 9:23 PM, Michael Bielski
<michael...@yahoo.com> wrote:
> I'll start by apologizing up front because I am very new to the Angular
> community. I've been through the tutorial and I guess I still just don't get
> it. I'm trying to create a resource and the problem that I have run into is
> that I need to get the value of a specific header when a GET call is made,
> and for the life of me I can't seem to figure out how to do this. I've been
> searching and reading all morning, but apparently my Google-Fu needs some
> work. Here's where I left off trying before posting:
>
> function loginController($scope, Logins, $http) {
> $scope.doLogin = function () {
> var myToken = Logins.get(function (getResponseHeaders) { return
> getResponseHeaders("Auth-Nonce"); });
> $scope.stats = myToken;
> }
> }
>
> angular.module('loginService', ['ngResource']).factory('Logins', function
> ($resource) {
> return $resource("/v1/rest/auth/:usrId/:pwdVal", { usrId: "", pwdVal: ""
> }, {
> checkUser: { method: "GET", params: { usrId: "@user", pwdVal: "@pwd"
> }, isArray: false, headers: { "Auth-Nonce-Response": "@nonce"} },
> getSeat: {},
> logOut: {}
> });
> });
>
> Any help would be greatly appreciated.
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/k9gIc_ZlljMJ.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.

Michael Svensson

unread,
Sep 19, 2012, 6:22:47 AM9/19/12
to ang...@googlegroups.com
Hi Pawel,
Like Michael Bielski, I too try to read the headers on a POST callback to figure out if the post succeeded or not. I've tried what you're suggesting but in my callbacks headers and status are never defined, only the data part. I created a jsFiddle on http://jsfiddle.net/pVPWu/

Any ideas?

//Michael

Michael Bielski

unread,
Sep 19, 2012, 11:10:01 AM9/19/12
to ang...@googlegroups.com
Hi Michael,

I still have yet to get the hang of resources and anything that comes out of a factory function, but I have had a lot of success with the $http calls, including the headers. Read the docs on $http (http://docs.angularjs.org/api/ng.$http) and see if that helps. Also, note the name of the return variables and the order in which they are listed: data, status, headers, config.

I tried to get your fiddle to run (http://jsfiddle.net/mbielski/pVPWu/1/) but wasn't successful. Maybe Pawel will have better luck than I did.

Michael Svensson

unread,
Sep 21, 2012, 4:50:01 AM9/21/12
to ang...@googlegroups.com
It seem I linked to the wrong fiddle. Let me update..

Michael Svensson

unread,
Sep 21, 2012, 7:30:52 AM9/21/12
to ang...@googlegroups.com
Updated the jsfiddle - http://jsfiddle.net/pVPWu/7/


On Wednesday, September 19, 2012 5:10:01 PM UTC+2, Michael Bielski wrote:

Pawel Kozlowski

unread,
Sep 21, 2012, 4:12:33 PM9/21/12
to ang...@googlegroups.com
Hi Michael(s)!

I think I own you apology as I've posted assuming things, without
checking properly before! Sorry about this.

To compensate here is a plunk showing how to get access to headers,
response status in case of success / error callback:
http://plnkr.co/edit/WOirj0?p=preview

There are several things to be noted here:
(1) callback arguments are different in case of success and error (!)
(2) success callback get 'data' and 'header access function' as
arguments (more about this in a second), nothing more (I'm not lying
this time :-) https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L388)
(3) error callback get the $http response as an argument
(4) headers argument is not an object (hash) but rather a function
(kind of getter) that needs to be called with a header name as an
argument, sth like:

$scope.header = headers('content-length');

Now, because success callback doesn't get response nor it status you
can't examine it. But somehow being in the success callback means that
the call succeed (actually AngularJS checks this: 200 <= status &&
status < 300, https://github.com/angular/angular.js/blob/master/src/ng/http.js#L82).

If you absolutely need access to the response status in the success
callback you will have to either an issue for this, roll out your own
$resource or hack the existing one....

Once again, sorry about the confusion and don't hesitate to ask
further questions if you need more help with this one...

Cheers,
Pawel

--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Michael Bielski

unread,
Sep 21, 2012, 4:19:25 PM9/21/12
to ang...@googlegroups.com
Hi Pawel,

You owe us nothing. Quite to the contrary, it is the two of us that owe YOU something: THANK YOU FOR EVERYTHING!!!

Very few people are more helpful and willing to try to help just about anybody. You're an AngularJS rock star, and don't let anybody tell you differently!

Michael Svensson

unread,
Sep 25, 2012, 2:44:12 PM9/25/12
to ang...@googlegroups.com
Ok, now I got it. Cheers from the Michael(s) !
Reply all
Reply to author
Forward
0 new messages