We are currently designing a REST API to access classical customer data.
One of the elements in the API are the assets of an user. The assets are added under a given service. The backend API will only add an asset to an user under a given service. So, there's no User–Asset relation, but a User–[Service]–Asset relationship.
Our URI's will look like this:
/users/{id}/assets/{id}/services/{id}
Uses of the API will know the asset id and the service id to create a new entry.
What we are struggling with is the creation of this relation.
One straightforward way would be to post the whole relation to /users/{id}/assets/
POST /users/{id}/assets
{asset:${id}, service:{id}, attribute1:"{var}", attribute2:"{var}"}
but then we are not actually creating an asset as the URI might indicate, but an asset-service relation.
As an alternative, we are considering POST'ing to the URI addressing the relation, like this:
POST /users/{id}/assets/{id}/service/{id}
{attribute1:"{var}", attribute2:"{var}"}
But in this case, the resource path /users/{id}/assets/{id}
will not exist before the POST and will be created as a side-effect.
Is POST'ing to a resource path that does not exist yet allowed at all?
Thanks for your thoughts,
Gerard.
Best Answer
It sounds like you're suggesting that, whenever a user first posts to a non-existent relation, you will create it as part of the post.
If you're asking whether this kind of create-on-access pattern is a valid, acceptable development pattern, the answer is yes it is - it's both valid, and a fairly common pattern to see.