REST Relationships – Understanding Relationships Besides Parent/Child

relationshipsrestweb services

This would seem to be a pretty basic question, but I can't seem to find appropraite guidelines for it. What is the "restful" way of POSTing a relationship between two entities that are not necessarily parent/child. As an example, consider customers and orders.

One could create a customer with a POST to /customer. However, what is the best way to create the order? One example would be a POST to /customer/123/order, but what if the order isn't assigned to a customer directly, and could exist on its own (in the case of a web-cart that allows for "anonymous" checkout)? Is the right way, then, to do POST /order?customerId=123

Further, consider the case that an order can be submitted separately from payments (let's say we are invoicing a customer). You could create the order (per either of the methods above), but how would we make the payment? POST /order/123/payment or POST /payment?orderId=123 (if we assume that a payment can be made for something other than an order, and one might want to get information about an order/payment without knowing the specific customer).

Best Answer

I would say that if an order is a first class object, use /order. If it needs to be associated with one or more customers, the customer ids should be an attribute of the order object.

Related Topic