Open fdipuma opened 9 years ago
:+1: for this. I was surprised that it's not possible to add custom methods/verbs. I was trying to do the exact same thing:
app.config(['RestangularProvider', function (RestangularProvider) {
RestangularProvider.addElementTransformer('photos', false, function (photo) {
photo.addRestangularMethod('like', 'like');
photo.addRestangularMethod('unlike', 'like');
photo.addRestangularMethod('publish', 'publish');
photo.addRestangularMethod('unpublish', 'unpublish');
return photo;
});
}]);
Which would allow something like :
LIKE /photos/1234
UNLIKE /photos/1234
UNPUBLISH /photos/1234
I manage this issue with this workaround. If you are using a framework that support method spoofing or if it supports method override you can try this.
For example i'm using laravel http://laravel.com/docs/5.0/routing#method-spoofing
RestangularProvider.addElementTransformer('photos', false, function (photo) {
photo.addRestangularMethod('like', 'post', '', {'_method' : 'LIKE'}, {'X-HTTP-Method-Override' : 'LIKE'});
photo.addRestangularMethod('unlike', 'post', '', {'_method' : 'UNLIKE'}, {'X-HTTP-Method-Override' : 'UNLIKE'});
photo.addRestangularMethod('publish', 'post', '', {'_method' : 'PUBLISH'}, {'X-HTTP-Method-Override' : 'PUBLISH'});
photo.addRestangularMethod('unpublish', 'post', '', {'_method' : 'UNPUBLISH'}, {'X-HTTP-Method-Override' : 'UNPUBLISH'});
return photo;
});
I used the same workaround with ASP.NET Web Api, but the X-HTTP-Method-Override header is not supported by many already existing API on the Web.
+1 LINK and UNLINK would be ideal for attaching/detaching relationships between two resources.
I agree, this would definitely be a good idea. And since $http
supports custom methods, this should be relatively simple to support. Would someone mind creating a PR with these changes?
+1 I need it too. The specifications of link is particular. You have no body unlike POST method but the uri "linked" or "unlinked" is placed in headers.
I didn't find any way to use a custom HTTP method with Restangular. This would be very useful for many use cases.
Example of HTTP methods not implemented:
It is semantically incorrect to use POST in those cases.
The possibility to create custom methods using custom verbs would solve this problem.