REST Shopping cart

后端 未结 5 1028
野的像风
野的像风 2020-12-08 16:48

Can a shopping cart be implemented using REST architecture constraints?

I would like to focus my question with respect to session state. In a typical MVC applicatio

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 16:56

    In REST applications, the session state is entirely managed by the client and the request must contain all the necessary information to be understood by the server. If the server requires authentication, for example, each request must contain the credentials.

    The REST stateless constraint is defined as the following:

    5.1.3 Stateless

    [...] each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is therefore kept entirely on the client. [...]

    However, the stateless constraint doesn't mean the server shouldn't store any data.

    In applications which the session state is managed by the server, it's a common approach storing the shopping cart data in the HTTP session. But a shopping cart is not a session state. And, probably, it should not be entirely managed by the client.

    In REST, a shopping cart, can be a resource identified by a URL such as /shopping-cart and operations can be performed over it. All the shopping cart data can be persisted to a database on the server.

    Any information that can be named can be a REST resource, even a shopping cart:

    5.2.1.1 Resources and Resource Identifiers

    The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time. [...]

    You can perform operations like these on your shopping cart:

    • GET /shopping-cart: Get the shopping cart.

    • POST /shopping-cart: Add an item to the shopping cart (sending some data with the item you are adding and the amount in the request body).

    • DELETE /shopping-cart/1: Remove an item with id 1 from the shopping cart.

    • PATCH /shopping-cart: Update the shopping cart (sending some data to be updated in the request body).

    • PUT /shopping-cart: Replace the whole content of the shopping cart (sending some data with the content of your shopping cart in the request body).

    • DELETE /shopping-cart: Remove the shopping cart

    • POST /shopping-cart/order: Order the shopping cart content

    So, observe the client won't store any information about the shopping cart at any time. All the information related to the shopping cart will be stored in the server.


    For more information about REST, I do recommend reading the chapter 5 of Roy T. Fielding's dissertation.

提交回复
热议问题