0

I have a REST Service that can be used to control databases, I want to allow calls to Stop & Start the databases, but was wondering what would be the correct Method?

By calling the Stop or Start Operation I am changing the state of the resource so a PUT seems sort of right, but is PATCH better or even POST?

Any suggestions?

sbarnby71
  • 584
  • 4
  • 7
  • 21
  • REST is for updating and retrieving resources/data. I would be surprised that it's used for controlling an application that way but maybe I'll learn something new. – Rob Jun 09 '16 at 14:42
  • It really depends on perspective. PATCH might make sense in that you are modifying the resource; but POST (start) and DELETE (stop) may make sense too. – RamblinRose Jun 09 '16 at 14:45
  • @RamblinRose `DELETE` might not a good option to stop the database... – cassiomolin Jun 09 '16 at 15:00
  • Rob - REST Services are used for way more than just updating resources and retrieving data. Tesla use their REST Service to allow a owner to start the engine, open the trunk, set the temperature etc. – sbarnby71 Jun 10 '16 at 06:59
  • Please let me know if my answer was useful for you. – cassiomolin Jul 07 '16 at 15:15

2 Answers2

0

Replacing the state of a resource

REST is protocol independent and is a resource-oriented architecture. When implementing REST applications over the HTTP protocol, for example, the resource is identified by the URI and the operation over the resource is expressed by the HTTP method.

PUT is the HTTP method used to replace the state of a resource and the new state of the resource will be expressed in the request payload using, for example, JSON and/or XML.

So you could consider the following design to start/stop a database:

PUT /databases/:id/status HTTP/1.1
Content-Type: application/json

{
    "value": "started"
}
PUT /databases/:id/status HTTP/1.1
Content-Type: application/json

{
    "value": "stopped"
}

To obtain the state of a resource, use GET:

GET /databases/:id/status HTTP/1.1

Response status codes

You certainly will need to inform your client about the result of the operation. To do it, use the HTTP response status codes.

A few status that might be useful:

  • 200: Use this status to indicate that the request has succeeded.
  • 202: Use this status code to indicate the request has been accepted for processing, but the processing has not been completed.
  • 204: Use this status code to indicate the server has successfully fulfilled the request and that there is no additional content to send in the response payload body.
  • 409: Use this indicates that the request could not be completed due to a conflict with the current state of the target resource.
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

Jim Webber explains that "HTTP is the application protocol of transferring documents" The transitions of state in your application are a side effect triggered by the document transfer.

Think old fashioned paper driven business office: the boss comes along and drops a TODO message in your inbox, which says "stop the database". As a side effect, you swivel your chair around and initiate the clean shutdown procedure.

Idiomatically, therefore, the representation you are sending to the REST server is that of the TODO message, and you are sending it to either (a) a resource that represents the "inbox" -- ie, a specific collection of TODO messages -- or (b) a resource that represents the TODO document itself.

I have a REST Service that can be used to control databases, I want to allow calls to Stop & Start the databases, but was wondering what would be the correct Method?

By calling the Stop or Start Operation I am changing the state of the resource so a PUT seems sort of right, but is PATCH better or even POST?

Since you are sending a complete message, rather than trying to make a modification to a message that the REST server already knows about, PATCH is not appropriate.

DELETE is also not appropriate - delete is analogous to destroying the TODO message in the inbox.

If the media type that you are using to represent application state at the client is HTML, then the verb you use shall be POST, because HTML doesn't support PUT.

If you are delivering a representation of a single message to a resource that represents a collection, then the verb you use shall be POST, because the semantics of PUT imply "create/overwrite" of a resource, and the semantic you are expressing is append.

If you are delivering a representation of a single message to a resource that represents that message alone (ie: you are doing a create of the message resource), then PUT is preferred to POST. The key idea here is that PUT promises that any side effects on the server are idempotent -- the side effect of successfully delivering N > 0 copies of the message are equivalent to the side effect of delivering exactly 1 copy. Using PUT as the verb shares that promise, not just with the client and server, but also with all of the intermediate connectors along the way.

Your idempotent resources can also support POST, and you can document in your API that the messages received are handled idempotently so that clients will understand, but there's no standardized way to inform the other connectors of this fact.

(Example: think about a form post in a browser. The resource at the server knows that the request can be handled idempotently. You can document in the html itself that hitting the button more than once is safe, but you don't have any way to tell the browser that, so the browser throws up a message to the user saying "re-POST might not be safe, are you sure Y/N?")

In summary, you want your choices of HTTP methods to agree with the uniform interface, so that the client, the server, and all of the components acting on the messages in between have a shared understanding of what's going on.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91