mardi 30 juin 2015

proper way to handle these RESTful calls for handling state

So, I have done a couple of projects where a call comes up for toggling state on an item. It is usually like whether an item is enabled or liked or something similar. It has to be a binary element though.

I have usually said that the proper way to do this is posting / deleting but that a quicker way is to do something like

/api/toggle_enabled/23/item

or

/api/toggle_liked/28/item

and have the state be reconciled on the server and return in the response the resulting state like:

{
 id:23,
 is_enabled: true / false
}

or

{
 id:28,
 is_liked: true / false
}

Usually other developers groan (as do I) when I see this but has always worked well and simplified client-side code by handling things like state for a user pressing a button multiple times very quickly. How do other developers handle this type of situation and is there another good option for handling? I know that this breaks RESTful principles but the simplicity seems to be worth it.

Aucun commentaire:

Enregistrer un commentaire