0

What if in application you can add new product in 2 ways:

  • send product details like: name, quantity
  • send: id of existing product, quantity

So, we should have "POST /products" endpoint for creation, but how to differentiate if we have 2 ways?

Sodiaan
  • 341
  • 1
  • 3
  • 10
  • The ideal choice would be to go with the POST in the first case and PUT in the second case when the client decides the name of the resource (I assume that the product id is the name). `POST /products {"name":"myName", "quantity":1}` `PUT /products/123456 { "quantity":1}` – MaVVamaldo Nov 29 '16 at 13:56
  • @MaVVamaldo the second way is not modification, is creation, it create new product with same parameters as product with that id except quantity – Sodiaan Nov 29 '16 at 13:58
  • I don't understand whether you are referring to my answer or your question. However PUTting a resource is a perfectly accepted way of creating it. The difference is that the client must know the name of the reource that's going to be created. See [here](http://stackoverflow.com/q/630453/740480) for details. – MaVVamaldo Nov 29 '16 at 14:11
  • @MaVVamaldo I got your idea, thanks. It is good one, but I think it could be solution only for this case, what if we will have 3,4 ways to create? – Sodiaan Nov 29 '16 at 14:15
  • one approch would be to exchange the same resource with only certain fileds filled. For example if you need to create this obj in the first case {a=2} and this in the second case {b=4}, you could use this resource {a, b} and sending {a=2, b=null} in the first case and the opposite in the second case – MaVVamaldo Nov 29 '16 at 14:25
  • I continue here. In addition, a resource and its representations are separate concepts. One resource MAY have different representations so it is ok for a server exposing the most meaningful representatino given a use context. So, to the limit, It would be ok to provide different JSON representation for the first and the second creation option. – MaVVamaldo Nov 29 '16 at 14:32

0 Answers0