简体   繁体   中英

HTTP POST or PATCH for REST API cart checkout and the payment

In short: I want to know if the HTTP method PATCH is ok for my scenario.

I have REST API which checkouts the cart and charges the customer . Essentially endpoint consists of couple steps:

  1. Get balance
  2. Charge
  3. Edit cart order status

Therefore the API charges the client ( changes his balance), then confirms the cart order ( changes the cart status to confirmed).

According to the old reference : What is the difference between POST and PUT in HTTP?

  • POST is used to create a resource or modify it
  • PUT is used to create resource if it does not exist or replace, also it is idempotent

According to the : https://www.baeldung.com/rest-http-put-vs-post

  • POST is only for resource creation
  • PUT definition is the same

Lastly according to the : https://www.baeldung.com/http-put-patch-difference-spring

  • PATCH is used for the partial update, can be not idempotent

Therefore in my scenario endpoint is not idempotent , I can cross out the PUT, but in this case I'm lost whenever should I use the POST(which in older SO question was used for update) or PATCH(which now used for partial update). I'm leaning towards the PATCH but it seems like PATCH is used for modification of specific entity, meanwhile POST seems to fit the case but it is no longer used for updating.

I read your use case is to submit a cart - meaning wise place an order.

Patch: It is used to partially update existing resources. There are two common ways of implementation with a Rest API: HTTP Patch RFC5789 and JSON Patch RFC6902. None fits your case, so Patch is not the method I would choose.

Post vs Put: Difference between Post and Put is idempotency. I assume a put endpoint would be idempotent while a post will be not. Therefore, I would decide put or post solely on your implementation. As you stated the API is not idempotent, so I would go with POST.

The operation to place the order updates their balance and updates their cart (removing the items purchased), but it should probably be considered primarily as creating an order. So I would say POST.

Unless you are considering their cart as a pending order and giving them a brand new "cart" with each subsequent visit/purchase. It is easier to separate the concept of cart and order, where they have different valid operations. The customer can update their cart, add / remove and create (POST) an order with it. The warehouse can update fulfil / update orders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM