$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
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.
This problem isn't just for Django people. We have a custom PHP webservice that was created with all trailing slashes.
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?
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?
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 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.
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_SLASHis 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');
}
]);
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.
is this still open?!
Fixed via #5560.
:confetti_ball:
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?