What's the justification behind disallowing partial PUT?

后端 未结 4 1268
一生所求
一生所求 2020-12-08 22:10

Why does an HTTP PUT request have to contain a representation of a \'whole\' state and can\'t just be a partial?

I understand that this is the existing definition of

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 22:49

    Short answer: ACIDity of the PUT operation and the state of the updated entity.

    Long answer:

    RFC 2616 : Paragraph 2.5, "POST method requests the enclosed entity to be accepted as a new subordinate of the requested URL". Paragraph 2.6, "PUT method requests the enclosed entity to be stored at the specified URL".

    Since every time you execute POST, the semantic is to create a new entity instance on the server, POST constitutes an ACID operation. But repeating the same POST twice with the same entity in the body still might result in different outcome, if for example the server has run out of storage to store the new instance that needs to be created - thus, POST is not idempotent.

    PUT on the other hand has a semantic of updating an existing entity. There's no guarantee that even if a partial update is idempotent, it is also ACID and results in consistent and valid entity state. Thus, to ensure ACIDity, PUT semantic requires the full entity to be sent. Even if it was not a goal for the HTTP protocol authors, the idempotency of the PUT request would happen as a side effect of the attempt to enforce ACID.

    Of course, if the HTTP server has close knowledge of the semantic of the entities, it can allow partial PUTs, since it can ensure through server-side logic the consistency of the entity. This however requires tight coupling between the data and the server.

提交回复
热议问题