[英]Should all the fields of a resource and its related resources be passed in a REST API PUT request?
Let's say I have a ticket & comment resource. 假设我有一张票和评论资源。 Tickets can have many comments. 门票可以有很多评论。 In your update endpoint HTTP PUT /api/tickets/<ticket_id>/
, should I require the client to pass all the fields of a ticket + ALL its comments when updating a ticket? 在更新端点HTTP PUT /api/tickets/<ticket_id>/
,我是否应该要求客户端在更新HTTP PUT /api/tickets/<ticket_id>/
所有字段及其所有注释?
I asked some developers and some of them said that I should just pass the fields that will be modified since its lighter, faster in terms of performance and easier to use. 我问了一些开发人员,他们中的一些人说我应该传递将要修改的字段,因为它更轻,性能更快,更容易使用。 And some said that I should pass all the fields of a ticket + ALL its comments since PUT request should be idempotent. 有些人说我应该通过票证的所有字段+所有评论,因为PUT请求应该是幂等的。 But my concern is when there are too many comments, the payload will be very big. 但我担心的是,当评论太多时,有效载荷会非常大。
Yes, since a PUT request should replace the entity-to-update in it's entirety. 是的,因为PUT请求应该替换整个实体到更新。 If you want to do a partial update of an entity, use a PATCH request. 如果要对实体进行部分更新,请使用PATCH请求。
Also see the rfc for reference 另请参阅rfc以供参考
PATCH Method for HTTP HTTP的PATCH方法
Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource modification. 扩展超文本传输协议(HTTP)的几个应用程序需要一个功能来进行部分资源修改。
The existing HTTP PUT method only allows a complete replacement of a document. 现有的HTTP PUT方法仅允许完全替换文档。 This proposal adds a new HTTP method, PATCH, to modify an existing HTTP resource. 此提议添加了一个新的HTTP方法PATCH来修改现有的HTTP资源。
So in your situation, it's probably a lot more efficient to use a PATCH request to do a partial update. 因此,在您的情况下,使用PATCH请求进行部分更新可能会更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.