Reputation: 239
I am looking for a good way to model commands that change the status of a resource in a REST api. I have scoured the internet and found various solutions, I would like to get feedback on the pros/cons of these.
Consider a resource that has an "isActive" state. The resource can be made active if its current "isActive" state is false and visa versa.
Solutions:
Client PUTs|PATCHes to the resource with the new state.
Client POSTs to resource/{id}/activate. (No body required.)
Client POSTs to resources/active or resources/inactive. (The body contains the id of the resource that should be added to the active resources collection)
Client POSTs or DELETEs to resources/{id}/isactive to indicate whether the resource is active or not.
Are they any other ways that you know of?
I quite like option 3, but have not seen this method anywhere online, so maybe it violates some REST principle!
Upvotes: 2
Views: 1053
Reputation: 48827
Quoting https://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#restful:
Treat it like a sub-resource with RESTful principles. For example, GitHub's API lets you star a gist with
PUT /gists/{id}/star
and unstar withDELETE /gists/{id}/star
.
Applying this approach to your case:
PUT /resources/{id}/active
DELETE /resources/{id}/active
Upvotes: 0