REST API – How to Implement a RESTful API That Updates a List with Exclusions and Additions?

djangorest

We have an application where there is an API to create a document with a POST and update it with a PUT. There is a separate API to share document with users.

Now, How should we approach updating the list of users who the document is shared with ?

Should it be a POST or PUT that actually deletes the whole association and replace it with new ?

How are things like these sanely implemented in a RESTful way ?

Best Answer

When using the HTTP protocol for your REST API (the common situation), PUT is used to replace a resource and POST to modify a resource.

If the list of document recipients is modeled as a sub-resource of your document, you can either use POST to add users to the list or PUT to replace the whole list, whatever is most convenient to you and/or the users of your API.

If the list of document recipients is not modeled as a sub-resource, but is a simple property of the document, then you should use POST (on the document) to modify the list without overwriting the rest of the document resource.

Related Topic