Groups should come first in URLs when you are adding a user to a group.
Group Resource:
{
"id": "group1",
"name": "Developers",
"users": [
{
"id": "user1",
"name": "John1"
},
{
"id": "user2",
"name": "John2"
}
]
}
User Resource:
{
"id": "user1",
"groups": "John1",
"users": [
{
"id": "group1",
"name": "Developers"
},
{
"id": "group2",
"name": "Testers"
}
]
}
Create a new group: POST https://api.myapp.com/groups
Request URL: https://api.myapp.com/groups
Request Body:
{
"name": "Developers",
}
Response Body:
{
"id": "group1",
"name": "Developers"
}
HTTP Status Code: 201
Create a new user and add him to existing group: POST https://api.myapp.com/groups/{groupId}/users
Request URL: https://api.myapp.com/groups/{group1}/users
Request Body:
{
"name": "John3"
}
Response Body:
{
"id": "user3",
"name": "John3",
"groups": [
{
"id": "group1",
"name": "Developers"
}
]
}
HTTP Status Code: 201
Create a new user & group: POST https://api.myapp.com/users
Request URL: https://api.myapp.com/users
Request Body:
{
"name": "John4",
"groups": [
{
"name": "Testers"
}
]
}
Response Body:
{
"id": "user4",
"name": "John4",
"groups": [
{
"id": "group2",
"name": "Testers"
}
]
}
Add an existing user to an existing group: POST https://api.myapp.com/groups/{groupId}/users/{userId}
Request URL: https://api.myapp.com/groups/{group2}/users/{user1}
Request Body: No Body
Response Body: No Body
HTTP Status Code: 200
Get a group: GET https://api.myapp.com/groups/{groupId}
Request URL: https://api.myapp.com/groups/{group1}
Response Body:
{
"id": "group1",
"name": "Developers",
"users": [
{
"id": "user1",
"name": "John1"
},
{
"id": "user2",
"name": "John2"
},
{
"id": "user3",
"name": "John3"
}
]
}
Get a user: GET https://api.myapp.com/users/{userId}
Request URL: https://api.myapp.com/users/{user1}
Response Body:
{
"id": "user1",
"name": "John1",
"groups": [
{
"id": "group1",
"name": "Developers"
},
{
"id": "group2",
"name": "Testers"
}
]
}