2

Several authors say that in the REST architecture, the POST method should be used to create a new resource, and only the PUT method is used to update an existing resource (or create a new one). But the problem is that the PUT method is idempotent, so if a resource has a date field named "updatedTime" that must be set in the server side for reliability, the updated operation is no longer idempotent (because the value of the "updatedTime" will always change in each new operation), so PUT can not be used, and as POST is used only to create a new object how to fix this?

Belin
  • 343
  • 1
  • 5
  • 17
  • There is nothing to fix. Both PUT and POST can be used to create or update. It is customary to use PUT to update and POST to create. Your question is unclear. – Idos Jan 21 '16 at 07:48
  • This post might be helpful http://stackoverflow.com/questions/2001773/understanding-rest-verbs-error-codes-and-authentication – Kaushik Jan 21 '16 at 07:49
  • I summarize my question in the hope that it will be more clear. If it says to update a resource only PUT should be used, what if we must implement a non idempotent update method, if only PUT must be use to make an update operation? – Belin Jan 21 '16 at 10:57

1 Answers1

1

AS per HTTP's definition of idempotent :

Like the definition of safe, the idempotent property only applies to
what has been requested by the user; a server is free to log each
request separately, retain a revision control history, or implement
other non-idempotent side effects for each idempotent request
.

So you're free to modify the updated time in an underlying server object as long as it doesn't affect the resource served by the HTTP server.

If you're concerned about breaking idempotency (although one could debate whether it is a real violation), I would therefore advise you to store updatedTime in a server object field but only expose information about it through an appropriate Last-Modified header instead of putting it in the response body that represents the resource.

As a side note, POST is not only to create resources (see the spec)

Community
  • 1
  • 1
guillaume31
  • 13,738
  • 1
  • 32
  • 51