Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST services - exposing non-data "actions"

I understand how to use REST for doing general entity interactions - using urls names to map to entities and the HTTP verbs to map to actions on those entities. But what is the generally accepted way of looking at "actions" more like RPC?

For example, let's say I want to send a command for the device to reset? There's no real "entity" here or do I do something like POST to http://mydevice/device/reset?

like image 923
ctacke Avatar asked Mar 15 '10 14:03

ctacke


People also ask

What are different types of requests in REST API?

The most common are: GET, POST, PUT, and DELETE, but there are several others. There is no limit to the number of methods that can be defined and this allows for future methods to be specified without breaking existing infrastructure.

What are RESTful APIs and its principles?

Full form of REST API is Representational State Transfer Application Programming Interface more commonly known as REST API web service. It means when a RESTful API is called, the server will transfer a representation of the requested resource's state to the client system.


2 Answers

/device/reset or /system/reset are ok.

The REST "design pattern" does encourage you to NOT use any verbs.. You could do:

POST http://mydevice/system/state    
<stateType>RESET</stateType>

Related information:

  • How to create REST URL’s without verbs?
  • Threads tagged with restful-url
like image 181
Marcus Leon Avatar answered Nov 03 '22 08:11

Marcus Leon


I don't think that's the case to use POST. The "RESET action" is a idempotent action (if you call it n times you will always get the same result), so IMHO you should use a PUT call instead POST (as POST is not idempotent).

Also, as you are Putting a resource, you can use

PUT http://system
<device>
  <status>RESET</status>
</device>

or

 PUT http://system/status/reset

But I think the first one is "more restful", since you are putting a resource, while the second one you just use the URL.

like image 37
Diego Dias Avatar answered Nov 03 '22 10:11

Diego Dias