Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actions vs. CRUD in REST

Is it appropriate to perform actions with REST, other than simple create (POST), read (GET), update (PUT), and delete (DELETE)? I'm kind of new to the whole RESTful theology, so bear with me, but how should I accomplish the following:

  • I have a web service that needs to talk to another web service. Web service A needs to "reserve" an object on Web service B. This object has a timeout of validity, but can be deleted immediately if need be. It's essentially a glorified permissions system which requires web services to reserve a space on web service B before taking any actions.

My initial thought was to 1. enable authentication of some sort, 2. in the serverside response to a GET call, reserve the space and return the result, and 3. provide immediate "unreservation" of the object via a DELETE call. Is this still being RESTful?

like image 404
Naftuli Kay Avatar asked Jul 13 '11 21:07

Naftuli Kay


People also ask

What are actions in REST API?

Actions are basically RPC-like messages to a resource to perform a certain operation. The “actions” sub-collection can be seen as a command queue to which new action can be POSTed, that are then executed by the API.

What is a CRUD action?

CRUD Meaning: CRUD is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete.

What are CRUD operations in REST API?

CRUD is an acronym for Create, Read, Update, and Delete commands. These four major functions are primitive guidance for software developers that interact with databases. CRUD operations are cyclic in nature rather than being an architectural system.

What are the principles of CRUD and how is it linked to REST?

Principles of CRUD As mentioned above, the principles of the CRUD cycle are defined as CREATE, READ/RETRIEVE, UPDATE, and DELETE: CREATE procedures generate new records via INSERT statements. READ procedures reads the data based on input parameters. Similarly, RETRIEVE procedures grab records based on input parameters.


2 Answers

Yes, it's OK to perform actions with rest. What matters is that these actions should be guided by the representations you exchange.

If you think about the way the web works (via a browser), you do this all the time: you get an HTML form that lets you choose a number of actions you can perform. Then, you submit the form (typically via POST) and the action is performed.

It's good to be able to use DELETE via a programmatic client (which is something that non-AJAX requests in browsers wouldn't support), but the overall approach of a RESTful system should be very similar to what you find for websites (i.e. the focus should be on the representations: the equivalent of web pages in your system).

GET shouldn't have side effects, so don't use GET to make the reservation itself, use something like POST instead.

like image 141
Bruno Avatar answered Oct 13 '22 00:10

Bruno


No - unlikely to be restful

From your description ...

2. in the serverside response to a GET call, reserve the space and return the result

GETs should be idempotent. For this reason alone, your service is unlikely to be restful because the state of the system after the first GET is different.

You really need to consider that a Reservation is a resource and should be created with a POST to a reservations container which will return the URI of the new resource in the Location header of the HTTP response. This UrI can be used by Get to return the resource and updated with a PUT

Post should be used to extend an existing resource and Put for replacing the state of a resource. In your case, consider the Post to be updating a list of Reservations and returning the URI of the new resource (not just the I'd). Put can be used for changing the state associated with the resource identified by the UR

like image 35
Chris McCauley Avatar answered Oct 12 '22 23:10

Chris McCauley