CanadaIT CanadaIT - 1 year ago 80
reST (reStructuredText) Question

What is the standard way to access a controller action that modifies server data but isn't a CRUD operation?

I have a Ruby on Rails API. One of the ActionRecord models is a transaction. I want to be able to use a link such as

, where
is the id of the transaction in the database.

As I understand it, a
request should be avoided since data on the server is modified through the action. Would I then use a
request or a
request? Is it okay if the request body is empty in the call?

Answer Source

To get a RESTful API, you have to squeeze everything into the CRUD paradigm. This is a well known disadvantage of REST. A non-CRUD operation is usually disguised as being a resource creation or a resource update, although it is counter-intuitive. In your case, the operation updates both the bank account and the transaction. REST does not have a specific way of updating two resources at once, so you have to choose one of the following imperfect solutions:

  1. regard your operation as an update of the transaction (PUT or PATCH)
  2. regard your operation as an update of the bank account (PUT or PATCH)
  3. regard your operation as the creation of a fulfillment resource (POST)

In case of a POST, it is allowed to have an empty body. In case of a PUT, the body represents the new version of the entire resource. In case of a PATCH, the body represents the updated part of the resource.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download