RUDful non-CRUD operations

What is the "RESTful" way of adding non-CRUD operations to the RESTful service? Let's say I have a service that allows CRUD access to such records:

GET /api/car/123 <- Returns information for the Car object with ID 123 POST /api/car <- Creates a new car (with properties in the request) PUT /api/car/123 <- Updates car 123 (with properties in the request) DELETE /api/car/123 <- Deletes car 123 POST /api/car/123/wheel/ <- Creates a wheel and associates it to car 123 

If I want to change the color of the car, I would just POST /api/car/123 and enable the POST variable for the new color.

But let me say that I want to buy a car, and this operation is more complicated than just updating the "user" record of the "car-owned" property. Is RESTful just something like POST /api/car/123/purchase , where "purchase" is essentially the name of the method? Or should I use my own HTTP verb like PURCHASE instead of POST ?

Or are operations other than CRUD completely beyond REST?

+85
rest web-services
Jul 27 '11 at 19:33
source share
4 answers

Think of a purchase as a business entity or resource in a RESTful dictionary. Moreover, the purchase purchase actually creates a new resource. So:

 POST /api/purchase 

will place a new order. Details (user, car, etc.) Must refer to an identifier (or URI) inside the content sent to this address.

It doesn’t matter that ordering a car is not just a simple INSERT in the database. Actually, REST is not viewing your database tables as CRUD operations. Logically, you create an order (purchase), but the server side can perform as many processing steps as it wants.

You can even abuse HTTP even more. Use the Location header to return a link to the newly created order, carefully select HTTP response codes to inform users about problems (on the server or on the client side), etc.

+55
Jul 27 '11 at 19:42 on
source share

RESTful, as I understand it, is that you do not need new HTTP verbs, where a noun somewhere will mean what you need to do.

To buy a car? Well, that is not so.

 POST /api/order 
+11
Jul 27 '11 at 19:41
source share

What you really do is create an order. Therefore, add another resource for the order and message and put it during the order process.

Think of resources, not method calls.

To complete the order, you will probably get POST / api / order // complete or something similar.

+4
Jul 27 '11 at 19:40
source share

I feel that the REST API helps in a lot more ways than just providing semantics. Therefore, you cannot choose the RPC style just because of some calls that seem to make more sense in the RPC style. For example, google maps api to find directions between two places. It looks like this: http://maps.googleapis.com/maps/api/directions/json?origin=Jakkur&destination=Hebbal

They could call it "findDirections" (verb) and saw it as an operation. Rather, they made a “direction” (noun) as a resource and processed the search directions as a request for a route resource (although inside there could not be any real resource called a direction, and it could be implemented by business logic to find directions based on parameters) .

+2
Jun 19 '15 at 12:02
source share



All Articles