Custom action in RESTful service
for pure Restful Design i'd recommend something like:
POST /cats/123/actions
with the body of (the type of action is defined in request :
{
"actionType": "jump",
"customActionParameter": "some value"
}
but this will be a overkill. so i found it easier to follow Google Api Design Guide for Custom Methods:
POST /cats/123:jump
this is the method google uses in it's cloud infrastructure Api
If cats/123
represents a resource then think about it this way: that resource can have many states (eating, walking, sleeping, jumping, pissing, ...). When you are designing an API using the REST architectural style, you want to permit a client application to make allowable requests to the resource that will change its state.
In the context of cats/123
, you could do this through a series of POST requests that will cause the state of the resource to change. Taking advantage of the hypermedia capability in REST you could create a process like the requests and responses shown below. Notice that the allowable links change as a response to the POST. Also, the client application would be coding to the properties contained in the Links array and not the actual URI's contained in the Href properties.
Request:
GET cats/123
Response:
{
"Color" : "black",
"Age" : "2",
"Links":[
{
"Food":"kibbles",
"Method":"POST",
"Href":"http://cats/123",
"Title":"Feed the cat"
},
{
"Scare":"yell real loud",
"Method":"POST",
"Href":"http://cats/123",
"Title":"Scare the cat"
}]
}
Request:
POST cats/123
{
"Food":"kibbles"
}
Response:
{
"Color" : "black",
"Age" : "2",
"Tummy" : "full"
"Links":[
{
"Sleep":"lap",
"Method":"POST",
"Href":"http://cats/123",
"Title":"Pet the cat"
},
{
"Scare":"yell real loud",
"Method":"POST",
"Href":"http://cats/123",
"Title":"Scare the cat"
}]
}