restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Allow the use of custom HTTP methods/verbs

Open fdipuma opened this issue 10 years ago • 6 comments

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:

  • COPY
  • LINK/UNLINK
  • LOCK/UNLOCK

It is semantically incorrect to use POST in those cases.

The possibility to create custom methods using custom verbs would solve this problem.

fdipuma avatar Mar 04 '15 10:03 fdipuma

:+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 avatar Mar 11 '15 10:03 michgeek

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;
    });

michgeek avatar Mar 11 '15 11:03 michgeek

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.

fdipuma avatar Mar 12 '15 08:03 fdipuma

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

joshbodine21 avatar Feb 10 '16 15:02 joshbodine21

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?

daviesgeek avatar Jun 17 '16 23:06 daviesgeek

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

kevin-lot avatar Sep 05 '16 13:09 kevin-lot