0

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.

sadxd
  • 71
  • 1
  • 8

2 Answers2

1

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.

oliver
  • 66
  • 4
  • What if I do not actually create any new values in the database, but only modification is applied. E.g. please ignore the architecture part, but lets say balance is edited and the cart status is edited, nothing is actually newly created, would it make sense to use the POST either way? – sadxd Jul 06 '22 at 13:19
  • 1
    I would also use POST or PUT for updates. Nothing states that you cannot. Patch is special kind of update where you only submit the changed values. When e.g. you want to limit the size of a request or to store on single field change. Sometimes it is overengineered to implement it e.g. when you only have less attributes, when you want it implement easy and fast, or other reasons. – oliver Jul 06 '22 at 17:07
0

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.

dchidelf
  • 3
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – ethry Jul 06 '22 at 06:34