John Kenn
John Kenn

Reputation: 1665

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?

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.

Upvotes: 7

Views: 3692

Answers (1)

Tim
Tim

Reputation: 43374

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.

Upvotes: 10

Related Questions