$resource POST removing trailing slash
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
@mzgol you should send a pull request. As for @seubert's idea, I'd like to know as well why striping the slash is a good idea. Looks like django users are having a hard time with this.
To be clear, while Django users may be the most visible developers encountering this, unless there's a good reason, $resource should not be changing URLs. A slash is often important to a server.
@ivancamilov It's not really worth creating a pull request; our change is dead simple, any core developer could apply it in a second. This might be more of a political issue than time-related.
It would indeed be nice to get some clarifying to why striping occurs, is it because of the parameter passing to the URL builder? At least supply user an option to turn it off in the module.
~~with angular 1.2.0 adding a space at the very end of the url fixes it: {url: '/user/:id/ '}~~ [edited] this only works for calls with no querystring params
@mpaolini Maybe I'm wrong, but adding space at the end breaks query with parameters (angularjs 1.2.0 rc3):
with space:
qqApp.factory('Dialing', ['$resource', ($resource) ->
return $resource('/qq/dialing/:dialingId/ ') #space
])
Dialing.query({org: org.id}) # ---> GET /qq/dialing/%20?org=1038 - incorrect
dialing.$save() # ---> POST /qq/dialing/ - correct
without space:
qqApp.factory('Dialing', ['$resource', ($resource) ->
return $resource('/qq/dialing/:dialingId/') # without space
])
Dialing.query({org: org.id}) # ---> GET /qq/dialing/?org=1038 - correct
dialing.$save() # ---> POST /qq/dialing - problem for me
@telminov you are right, in the end I had to remove all trailing slashes in my django urlconf and also set APPEND_SLASH = False in my django settings to make angular happy
RC3 still has this issue and it would be awesome if ngResource respected the exact URL it is provided.
@Cinamonas well, they obviously won't change that for 1.2.0 as it's a breaking change and a lot of code could break because of that. Too late for that now.
@mzgol Adding an option for opt-out wouldn't break anything.
@mik01aj Ah, that's true. But it won't make it to 1.2.0 anyway, they're not accepting new features now.
@mzgol - I went ahead with the approach you mentioned in your earlier comment (ie. commenting out the line where the trailing slash gets stripped). You also mentioned that a lot of code could break because of that. I couldn't think of any cases that this change would break anything. Could you please enlighten me with a few examples? Thanks.
@tamakisquare That's simple, any code that would pass URLs with trailing slashes in some places and without in the others would break if the normalization process would be turned on. '$resource' will either have to be changed in a non-patch version bump or via adding an option to disable normalization but not by default.
Please add the option to disable normalization.
+1 for adding an option. This seems too dumb an issue to keep.
FWIW you can hack the behavior via double escaping the trailing slash, like:
$resource('/your/sane/api/endpoint\\/')
@casio thanks for the tip - I just ran into this issue with a Flask backend.
@casio Thanks for that tip. This workaround seems to break in Firefox, though.
As @nvie has indicated the workaround by @casio does not work in firefox,
I have come up with another solution that works in ff.
Add a variable onto the end of the url:
$resource('/your/sane/api/:endpoint')
and then set the variable with the trailing slash:
params : {endpoint : "endpoint/",}
but then again this isn't very nice either.
I took a different approach to remedy this incompatibility issue with my django backend. Just thought to share with the folks encountering the same problem for the time being. I wrote a custom response interceptor that adds the trailing slash if the url doesn't already have one. Note that the interceptor skips the requests that are configured to use $http cache.
I think the best fix is adding APPEND_SLASH = False in your django settings.