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

Time:07-06

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.

CodePudding user response:

This updates their balance and updates their cart, but it should probably be considered primarily as creating an order. So I would say POST.

CodePudding user response:

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.

  • Related