I'm having troubles with designing a rest api for the following scenarios:
I was thinking of doing it as below:
POST https://api.myapp.com/users (creates new user and new group, adds user to the group)
PATCH https://api.myapp.com/groups/{id} (creates new user, adds user to existing group) I don't like it that I create a new user via /groups endpoint.
POST https://api.myapp.com/groups/{id} (creates new group, adds user to the group)
PATCH https://api.myapp.com/groups/{id} (adds user to existing group)
I don't know how and if I should express in the api that users and groups depend on each other. Something like:
/users/{id}/groups /groups/{id}/users
If this is better approach then what should come first groups or users?
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"
}
]
}