mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

Allow the use of custom HTTP methods/verbs #1072

Open fdipuma opened 9 years ago

fdipuma commented 9 years ago

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.

michgeek commented 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

michgeek commented 9 years ago

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;
    });
fdipuma commented 9 years ago

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.

joshbodine21 commented 8 years ago

+1 LINK and UNLINK would be ideal for attaching/detaching relationships between two resources.

daviesgeek commented 8 years ago

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?

kevin-lot commented 8 years ago

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