Let’s assume I have two entities – project team and employee. Each employee could be part of multiple teams and each team can have multiple employees as team members. I need to provide REST API to manipulate teams, employees and relationships between them.
I have identified 3 resources – team, employee and member (association between team and employee), which is sub-resource of team. The reason I chosen to have member as sub-resource is purely based on lifecycle of this resource. Whenever team is removed members are removed as well as they don’t have meaning outside of team itself.
I expose following API (relevant ones):
POST /teamscreates new team record with name, department ID, etc.POST /teams/{name}/memberscreates association between team identified by name and particular employee, so the input data contains employee ID
I also need to provide API to update department ID and other attributes of the team in one request. Looks like PUT is natural choice but semantics of PUT is pretty clear – I have to replace whole resource, which in this case means replacing all members sub-resources as well.
What method (or approach) should I use when I only want to update team’s attributes while preserving member associations? Please keep in mind that I also want this request to be idempotent.
I have never heard anyone make this association before. If do
PUT /Fooin my opinion it says absolutely nothing about/Foo/bar. Just because resource can be accessed through a hierarchal URI space does not infer any additional relations between those resources.I have heard of people doing the opposite scenario where you do
PUT /Foo/barand if the server knows that this will impact the state of/Fooyou can include a Content-Location header that points to/Footo allow intelligent caches to invalidate/Foo. However, the Content-Location is needed to explicitly create the relationship between the two resources.