Should all the fields of a resource and its related resources be passed in a REST API PUT request?

老子叫甜甜 提交于 2019-12-11 05:55:16

问题


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?

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. But my concern is when there are too many comments, the payload will be very big.


回答1:


Yes, since a PUT request should replace the entity-to-update in it's entirety. If you want to do a partial update of an entity, use a PATCH request.

Also see the rfc for reference

PATCH Method for HTTP

Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource modification.

The existing HTTP PUT method only allows a complete replacement of a document. This proposal adds a new HTTP method, PATCH, to modify an existing HTTP resource.

So in your situation, it's probably a lot more efficient to use a PATCH request to do a partial update.



来源:https://stackoverflow.com/questions/30118138/should-all-the-fields-of-a-resource-and-its-related-resources-be-passed-in-a-res

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!