angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$resource POST removing trailing slash

Open msurdi opened this issue 14 years ago • 52 comments

I have a module where I define a service like this:

.factory("Charts", function ($resource) { return $resource("/api/charts/:chartId", { }, {}); })

Then, on the controller I do:

var chart = new Charts({ name:$scope.name, query:$scope.query, since:$scope.since, since_unit:$scope.since_unit, interval:$scope.interval } ); chart.$save();

And on the server I'm receiving the POST to /api/charts instead of /api/charts/ that is what (I think) it should be.

There is a discussion in the list:

https://groups.google.com/forum/?fromgroups#!topic/angular/taypgj_D3YQ

msurdi avatar May 27 '12 18:05 msurdi

Pull request on this issue: https://github.com/angular/angular.js/pull/1064/files

umurkontaci avatar Jun 17 '12 13:06 umurkontaci

we just ran into this with django, it was a bit of a tussel, with angular stripping off the slash and django (by default) was adding slash it back end and doing a redirect. Even after changing django default, I ran into an inconsistency with django-tastypie and ended up modifying ngResource to append a slash to make all parties happy.

a little differen than fastreload's

return url + "/" + (query.length ? '?' + query.join('&') : '');

I think maybe an param option might be nice.

--thx

dan

dandoyon avatar Jun 26 '12 23:06 dandoyon

I have done a quick hack on the issue but what I think is that frameworks should treat urls as is. Adding or removing a character from url actually means creating a completely new url and hoping that server will understand what we want.

umurkontaci avatar Jun 27 '12 05:06 umurkontaci

I think the convenience of providing the hash with bind elements (as opposed to having a url template), means, that angular needs to muck with the url. I think they tried to cover too many cases and instead should probably just tell the user. What the url will look like in the various cases or provide options to dictate what should happen. IMHO

dandoyon avatar Jun 27 '12 15:06 dandoyon

Yes, but they could also make their internal interpretation of the urls and use the original url while contacting server. I have not examined the inner workings of url interpretation and routing yet this still does not legitimize changing urls without explicit order from user/coder.

Each server can have different types of interpretation urls and a client side framework should be able to communicate with them with no hassle or source changing. For example, Django has APPEND_SLASH setting which uses HTTP 302 redirection to append slash to urls without slash. This works with GET method but not with others (POST,PUT,DELETE) because redirection cannot and will not pass the data to the new URL.

umurkontaci avatar Jun 30 '12 12:06 umurkontaci

I think these are great points to bring up. Don't know if you saw but there was a fairly long thread (a number of months ago) on ngResource rewrite and request from comments from Misko. Given the diverse response, I he decided to table things until future time.

dandoyon avatar Jun 30 '12 21:06 dandoyon

I haven't seen that thread, I am also not using angularjs for that long time, yet the claims that I have said are not design philosophies which is not related to a particular framework (as you said). Hopefully developers will see this issue and change their designs until new development cycle.

umurkontaci avatar Jul 03 '12 06:07 umurkontaci

Is there any more discussion on this? It would seem strange that the framework would enforce a certain type of url.

stryderjzw avatar Aug 21 '12 07:08 stryderjzw

Not that I know. No active developers have responded to this issue yet. I think that they would agree to that it is wrong to enforce a certain type of url but this part of the framework (ngResource, http) might not be loosely coupled with the rest of the framework and it may require more than a couple of changes for it to work. But that's just a hopeful assumption.

umurkontaci avatar Aug 21 '12 11:08 umurkontaci

There are many deficiencies in $resource. We are trying to figure out what to do with it. in the meantime we recommend that you create resources manually, which gives you full control over everything.

See: http://stackoverflow.com/questions/11850025/recommended-way-of-getting-data-from-the-server/11850027#comment15909222_11850027

On Tue, Aug 21, 2012 at 4:20 AM, Umur Kontacı [email protected]:

Not that I know. No active developers have responded to this issue yet. I think that they would agree to that it is wrong to enforce a certain type of url but this part of the framework (ngResource, http) might not be loosely coupled with the rest of the framework and it may require more than a couple of changes for it to work. But that's just a hopeful assumption.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/992#issuecomment-7898116.

mhevery avatar Aug 21 '12 17:08 mhevery

That would be a proper workaround meanwhile. Thanks for the heads up

umurkontaci avatar Aug 21 '12 18:08 umurkontaci

Thanks for the responses! Sorry to ask this here, but following the example, Book is undefined for me in my controller. Any suggestions?

stryderjzw avatar Aug 22 '12 03:08 stryderjzw

@stryderjzw the factory is missing return Book, I updated the SE.

mhevery avatar Sep 04 '12 21:09 mhevery

As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.

Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.

Thanks!

btford avatar Aug 24 '13 00:08 btford

Hi. This issue still persists in 1.0.8. I'm using $resource to get content from a third-party that needs the URLs to have a trailing slash, and all the requests are being made without the slash.

Can we reopen this issue?

ivancamilov avatar Aug 24 '13 20:08 ivancamilov

I'll just add this is also a problem in 1.2.0 rcs. This causes problems with many backends that require a trailing slash at the end of requests, $resource shouldn't be stripping that (or at least it should be possible to configure it so that it puts the trailing slash instead of removing it).

mgol avatar Sep 23 '13 16:09 mgol

This is a big problem when using a django backend, which uses URLs with slashes by default. In most cases it responds with a redirect to an URL with trailing slash (which is OK, aside from slowing down the app a bit), but it's impossible when we're sending POST data (and that's a real problem).

mik01aj avatar Sep 23 '13 16:09 mik01aj

Is there a reason for $resource not respecting the trailing slash? As reported above this is a real issue with Django's POST handling as the server just responds 301 Moved Permanently for a request missing the slash.

aleksihakli avatar Sep 27 '13 12:09 aleksihakli

We resorted to use a forked version of ngResource for now, with this line: https://github.com/angular/angular.js/blob/6a8edc1d43aca7c5a92f86309b1bb1d5f9968442/src/ngResource/resource.js#L375 commented out.

mgol avatar Sep 27 '13 12:09 mgol

@btford Can we get an explanation from a core dev regarding this? It's a lingering and festering issue that boils down to one line. I can't find any real reason for it and it's quite obviously a problem for multiple people. It shouldn't really be necessary for me to go use Restangular or drop down to $http just because of this one thing, IMO.

Thanks!

seubert avatar Sep 29 '13 19:09 seubert