问题
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