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

That sometimes isn't an option. The best fix would be having this be configurable in the $resource() constructor, e.g.:

$resource(url[, paramDefaults][, actions][, options]);

Where options could be:

{
  trailingSlash: true/false/undefined,
}

When the trailingSlash option isn't specified, or is undefined, it's the current behaviour. When true, it could always append if necessary, when false, always stripped when necessary.

nvie avatar Dec 04 '13 10:12 nvie

This problem isn't just for Django people. We have a custom PHP webservice that was created with all trailing slashes.

jdhiro avatar Dec 06 '13 09:12 jdhiro

My pull request to attempt to fix this for once and for all is #5560.

This should support the following configuration:

app.config(function($resourceProvider) {
  $resourceProvider.defaults.stripTrailingSlashes = false;
});

or per instance, using a new, fourth, optional argument to the $resource() constructor:

var CreditCard = $resource('/some/:url/', ..., ..., {
    stripTrailingSlashes: false
});

Any feedback, anyone?

nvie avatar Dec 29 '13 10:12 nvie

This is the stupidest bug ever, angular has no business editing urls that have been explicitly defined by the developer. And why was this ever necessary in the first place? And 2 years to fix this?

j0hnsmith avatar Jan 19 '14 23:01 j0hnsmith

Thanks for being super constructive @j0hnsmith :) This has been triaged for 1.2.x, and it is potentially a breaking change for people who are depending on this functionality unwittingly!

If you can put together a PR to make this work for you, you could let people dog-food it so that we can get a bit more certainty of it, and that would be awesome! We can try to look at it for 1.2.10, if not 1.3

(Oh, and just to correct you, this isn't really "angularjs" core, this is ngResource, which you are not at all forced to use with Angular)

caitp avatar Jan 20 '14 00:01 caitp

@caitp Sorry, don't mean to sound negative, I was just totally astonished by the situation, should've waited 5 mins before I pressed the comment button. After I posted the comment I did see that #5560 has been triaged. I look forward to the change making the grade.

j0hnsmith avatar Jan 20 '14 00:01 j0hnsmith

Until @nvie 's patch is in, the following will do for a workaround, even in firefox.

  • Note: This will remove an ending backslash from all urls. If you need that functionality in your application, don't use this.
  • Django users: this will be making it into djangular very soon, when settings.APPEND_SLASH is true.
angular.module('ngResource').config([
    '$provide', '$httpProvider',
    function($provide, $httpProvider) {
        $provide.decorator('$resource', function($delegate) {
            return function() {
                if (arguments.length > 0) {  // URL
                    arguments[0] = arguments[0].replace(/\/$/, '\\/');
                }

                if (arguments.length > 2) {  // Actions
                    angular.forEach(arguments[2], function(action) {
                        if (action && action.url) {
                            action.url = action.url.replace(/\/$/, '\\/');
                        }
                    });
                }

                return $delegate.apply($delegate, arguments);
            };
        });

        $provide.factory('resourceEnforceSlashInterceptor', function() {
            return {
                request: function(config) {
                    config.url = config.url.replace(/[\/\\]+$/, '/');
                    return config;
                }
            };
        });

        $httpProvider.interceptors.push('resourceEnforceSlashInterceptor');
    }
]);

brian-montgomery avatar Jan 22 '14 21:01 brian-montgomery

I've been dealing with this issue while developing server-side support for $resource in Django. (crud view in django-angular)

The simplest workaround for Django is to not use trailing slashes in views used with $resource. Note that APPEND_SLASH only appends trailing slash when the non-trailing version of the url doens't match any regex in the urlconf. But since not using trailing slashes is kinda anti-convention, it's better to make them optional rather than removing them.

E.g.

url(r'^angular/?$', AngularView.as_view()),

I'm in favour of that rather than monkey-patching $resource js. Anyway I hope this will be fixed soon.

jkosir avatar Mar 06 '14 10:03 jkosir

is this still open?!

FrancisVarga avatar Mar 24 '14 14:03 FrancisVarga

Fixed via #5560.

mgol avatar Apr 09 '14 22:04 mgol

:confetti_ball:

btford avatar Apr 09 '14 22:04 btford

Unreal. Spent I don't want to admit how long wasting my time trying to figure out what I was doing wrong only to find this. Why on earth was an explicitly set string being messed with in the first place?

jfbrennan avatar Nov 19 '14 05:11 jfbrennan