Reputation: 1879
Hello and happy January!
Reviewing my recently created a very simple RESTful API, I am wondering if I did the RESTful API signature for its POST.
This RESTful API queries a very simple MongoDB collection Apps
, and its columns are:
_id
, (appId
)siteId
(required)accountId
(required)provider
(required)description
Index:
provider
, siteId
, accountId
: unique
Data structure:
siteId
) has multiple Accounts (accountId
).accountId
) has multiple Apps (appId
).The paths for HTTP Verbs GET
DELETE
and PATCH
handling items in the collection Apps
are:
GET /api/v1/sites/:siteId/accounts/:accountId/apps
DELETE /api/v1/sites/:siteId/accounts/:accountId/apps
PATCH /api/v1/sites/:siteId/accounts/:accountId/apps
My quandary is a follows: I want to POST
a new app that will be associated with siteId
+ accountId
.
I am wondering if how I defined this POST
path makes sense, because the new app may be adding to this collection either siteId
or accountId
for the first time.
This is what I had implemented for HTTP Verb POST
:
POST /api/v1/sites/:siteId/accounts/:accountId/apps
params: {
siteId: string,
accountId: string,
},
body: {
provider: '[** Provider **]',
description: '[** Description **]',
siteId: '[** Site ID **]',
accountId: '[** Account ID **]'
},
response: new `appId`
Or should it be (which I am starting to lean towards):
POST /api/v1/apps
body: {
provider: '[** Provider **]',
description: '[** Description **]',
siteId: '[** Site ID **]',
accountId: '[** Account ID **]'
},
response: new `appId`
Recommendations are truly welcomed!
Upvotes: 0
Views: 74
Reputation: 2653
First of, it would be a little odd to have the siteId and accountId both in the url and the body. The url suggests you are creating an app for a specific site, and then the site is a variable in the message body, meaning it could be any site. This would only lead to confusion.
Do you want someone to be able to register a new app for any site/account combination? Or would someone go from selecting a site to registering a new app for that site. In the first case I would put it all in the body, in the second case go with the site in the url.
Considering RESTfulness; when you use the api to find sites or a specific site before you register an app to a specific site, the 'site' object in the response would have a reference to where one would register an app for that site. This link would probably contain the siteId. However if it were just a url where you could register any site, the siteId would have no place in the url and not even be part of that specific response.
I think I would take account out of the url regardless, this seems to be that it would always be the same and just something you have to pass on as a variable.
Upvotes: 2