Why HTTP method PUT should be idempotent and not the POST in implementation RestFul service?

Sam :

There are many resources available over the internet wherein PUT vs POST is discussed. But I could not understand how would that affect the Java implementation or back end implementation which is done underneath for a RestFul service? Links I viewed are mentioned below:

https://www.keycdn.com/support/put-vs-post/

https://spring.io/understanding/REST#post

https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

http://javarevisited.blogspot.com/2016/10/difference-between-put-and-post-in-restful-web-service.html

For example let's say there is a RestFul webservice for Address. So POST /addresses will do the job of updating the Address and PUT /addresses/1 will do the job of creating the one. Now how the HTTP method PUT and POST can control what weservice code is doing behind the scenes?

PUT /addresses/1 

may end up creating multiple entries of the same address in the DB.

So my question is, why the idempotent behavior is linked to the HTTP method?

How will you control the idempotent behavior by using specif HTTP methods? Or is it that just a guideline or standard practice suggested?

I am not looking for an explanation of what is idempotent behavior but what make us tag these HTTP methods so?

Daniel Boczek :

This is HTTP specific. As RFC linked by you states that https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html (see up to date RFC links at the bottom of this answer). It is not described as part of REST: https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

Now you wrote,

I am not looking for an explanation of what is idempotent behavior but what make us tag these HTTP methods so?

An idempotent operation has always the same result (I know you know it), but the result is not the same thing as the HTTP response. It should be obvious from HTTP perspective that multiple requests with any method even all the same parameters can have different responses (ie. timestamps). So they can actually differ.

What should not change is the result of the operation. So calling multiple times PUT /addresses/1 should not create multiple addresses.

As you see it's called PUT not CREATE for a reason. It may create resource if it does not exist. If it exists then it may overwrite it with the new version (update) if its exactly the same should do nothing on the server and result in the same answer as if it would be the same request (because it may be the same request repeated because the previous request was interrupted and the client did not receive response).

Comparing to SQL PUT would more like INSERT OR UPDATE not only INSERT or UPDATE.

So my question is, why the idempotent behavior is linked to the HTTP method?

It is likned to HTTP method so some services (proxies) know that in case of failure of request they can try safely (not in the terms of safe HTTP method but in the terms of idempotence) repeat them.

How will you control the idempotent behavior by using specif HTTP methods?

I'm not sure what are you asking for. But:

  • GET, HEAD just return data it does not change anything (apart maybe some logs, stats, metadata?) so it's safe and idempotent.
  • POST, PATCH can do anything it is not safe nor idempotent
  • PUT, DELETE - are not safe (they change data) but they are idempotent so it is "safe" to repeat them.

This basically means that safe method can be made by proxies, caches, web crawlers etc. safely without changing anything. Idempotent can be repeated by software and it will not change the outcome.

Or is it that just a guideline or standard practice suggested?

It is "standard". Maybe RFC is not standard yet but it will eventually be one and we don't have anything else we could (and should) follow.

Edit:

As RFC mentioned above is outdated here are some references to current RFCs about that topic:

Thanks to Roman Vottner for the suggestion.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=461314&siteId=1