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

@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.

ivancamilov avatar Sep 29 '13 19:09 ivancamilov

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.

seubert avatar Sep 29 '13 19:09 seubert

@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.

mgol avatar Sep 29 '13 20:09 mgol

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.

aleksihakli avatar Sep 30 '13 14:09 aleksihakli

~~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 avatar Oct 19 '13 06:10 mpaolini

@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 avatar Oct 21 '13 11:10 telminov

@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

mpaolini avatar Oct 25 '13 22:10 mpaolini

RC3 still has this issue and it would be awesome if ngResource respected the exact URL it is provided.

Cinamonas avatar Oct 28 '13 10:10 Cinamonas

@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.

mgol avatar Oct 28 '13 11:10 mgol

@mzgol Adding an option for opt-out wouldn't break anything.

mik01aj avatar Oct 28 '13 12:10 mik01aj

@mik01aj Ah, that's true. But it won't make it to 1.2.0 anyway, they're not accepting new features now.

mgol avatar Oct 28 '13 12:10 mgol

@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 avatar Oct 28 '13 19:10 tamakisquare

@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.

mgol avatar Oct 28 '13 20:10 mgol

Please add the option to disable normalization.

blaise-io avatar Nov 12 '13 12:11 blaise-io

+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 avatar Nov 12 '13 17:11 casio

@casio thanks for the tip - I just ran into this issue with a Flask backend.

teddyhwang avatar Nov 12 '13 21:11 teddyhwang

@casio Thanks for that tip. This workaround seems to break in Firefox, though.

nvie avatar Nov 27 '13 19:11 nvie

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.

mr-love avatar Dec 04 '13 02:12 mr-love

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.

tamakisquare avatar Dec 04 '13 08:12 tamakisquare

I think the best fix is adding APPEND_SLASH = False in your django settings.

mpaolini avatar Dec 04 '13 10:12 mpaolini