Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement a RESTful toggle-action?

Tags:

I'm doing a rewrite of an old Rails application and I thought I should do it in a RESTful manner, as a learning experience if nothing else.

I've reached some actions that toggles a boolean value, for example if an article is published or not.

Before I had a couple of actions: toggle_published, publish and unpublish.

They were very easy to use: i just made a link to them in the article-list.

How would you do the same thing in a RESTful manner?

Should I use the update-action, and build a mini-form to replace each link that I used before? I don't particulary like that idea.

like image 482
Jonatan Avatar asked Jul 16 '10 15:07

Jonatan


People also ask

What is RESTful API action?

Actions are the ability to connect anywhere on the web. The REST Action is the place to send REST requests in order to get data from Web services (APIs etc.) or post data in Web services.


1 Answers

Just a notice:

A toggle method is not RESTful, because the HTTP PUT verb is supposed to be idempotent (see a.o. http://en.wikipedia.org/wiki/Idempotence#Examples). This means that no matter how often you execute a method, it should always give the same result. A toggle method does not adhere to this principle, as it does not give the same result if you execute it once comparing to executing it twice.

If you want to make it RESTful, you should create two methods: one for setting and one for unsetting.

Making an application RESTful does not only mean that you should use the correct HTTP verb.

like image 172
Van der Hoorn Avatar answered Oct 04 '22 17:10

Van der Hoorn