Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$resource should provide http status codes or at least a better option to include them #8439

Closed
sethen opened this issue Jul 31, 2014 · 6 comments

Comments

@sethen
Copy link

sethen commented Jul 31, 2014

Hello all,

$resource for creating resource objects should allow you to retrieve the status codes coming back from REST endpoints. While there is interceptor with individual rest verbs while setting up $resource, that becomes a pain to add it each time. It should be as simple as passing an argument to the constructor or being able to modify the $resource globally so all responses give back the status. What I have found is trying to use interceptors with $resource doesn't work as expected.

Literally, all I am doing is this to currently get back the status for $resource when the data comes back:

'get': {
    interceptor: {
        response: function (response) {
            return response;
        }
    }
}

As I mentioned earlier, this is a bit tedious.

In my case, I don't want to use $http because of all of the benefit I get from using $resource in the first place.

@petebacondarwin
Copy link
Contributor

The success callback parameter to each call to a resource is called with the value and the response headers. Is this enough?

@sethen
Copy link
Author

sethen commented Aug 12, 2014

I have tried using the success callback with $resource and all I seem to get is the data and not the headers. Is this intentional with $resource?

@btford btford removed the gh: issue label Aug 20, 2014
@ianvonholt
Copy link

Having the header status codes within the resource response would be fantastic. I find adding custom interceptors into each resource bloats the code quite a bit.

With my application, I use standard HTTP/1.1 responses on the back-end.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Not everything returns a pure status 200.

You CAN get some of the header information in your success callback by doing the following:

function(response, status) {
    var statusHeader = status(); // Calls http.js - parseHeaders(headers)
    // Output 
    console.log(statusHeader); 
}

However, I've never actually gotten the Status from this function.

In the end I just did a down and dirty addition into the data object being returned by resource, regardless of there being data or not.

At the following:
https://github.com/angular/angular.js/blob/v1.2.23/src/ngResource/resource.js#L530

Insert a quick:

if (data) {
    data['status'] = response.status;
 } else {
    data = {'status' : response.status};
 }

Every single response will now have response.status. Then, when calling the resource you can match the success codes for whatever you would like.

I should also note, that your return data from the server should NEVER have a status variable, otherwise it will be replaced with the response.status value.

@sethen
Copy link
Author

sethen commented Aug 28, 2014

I guess I am sort of wondering what the decision to leave out the header information was in the first place? Seems like it should be there.

@SekibOmazic
Copy link
Contributor

Dupe of #8341 ?

@pkozlowski-opensource
Copy link
Member

Yes, this is duplicate of #8341

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants