CanadaIT CanadaIT - 8 days ago 7
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

http://localhost:3000/transactions/1/fulfill
, where
1
is the id of the transaction in the database.


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

Answer

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.