Regarding the principle of “stateless communication”

One of the key principles of the rest is to not maintain state on the server or not report without it. I want to see how this principle works in a shopping cart? So, let's say if the user adds products to the basket from the shopping site. In my opinion, the server will have some implementation to have a shopping cart in the session area, and the user will send a message to add the product to the shopping cart. For example, / shoppingcart / products / 1. Now the products remain added to the shopping cart (but are not stored in the database) until the user confirms the order. During checkout, the cart items are stored in a database on the server side.

Does this approach (putting an item in the shopping cart on the server side, which is in the session area) violate the principle of rest? If so, how can we implement the addition of the product to the shopping basket, observing the principle of "statelessness"?

+4
source share
1 answer

The basket should be processed as a resource, and products added / removed in the same way as you can add or remove associations between any two resources in REST. Instead of the customer saying, “Now buy items in my basket,” the customer should say, “Now buy goods in basket # 187462.” Assign a URL to each basket, and your actions will act on this resource, and not on any product array tied to the current session.

An alternative, which is also stateless, is that the client tracks all the items in the basket, but this means that the user cannot leave the basket (refuse it) on one computer and resume purchases on another device.

Addendum: Remember that authority / access control can be assigned independently. Of course, each cart has a URL, but make sure that the registered user can see only the cart resources they created.

+2
source

Source: https://habr.com/ru/post/1495307/


All Articles