REST api, POST entity with relationships?

给你一囗甜甜゛ 提交于 2019-11-30 18:59:49

I would suggest something like this.

POST /musicstore/artist/343/albums

{
    "Name":"AlbumName",
    "Description":"Some description for the album",
    "ReleaseYear": "1992",
}

The act of creating a resource as a child of the collection of albums for the artist 343 implicitly creates the relationship between the artist and the album. There is no need to specify it in the payload.

I think that both options are correct and it's up to you which one to use. The second one is more like HATEOAS.

Albums and Artists are two separate entities, you shouldn't post all the artist data into Albums, just its ID. Therefore you first create the artist if it doesn't exist, and after that you post the album details, artist_id being one of its properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!