To create a new resource item, post to the resource type link with a request body containing type and attributes.
POST /api/v1/jad/genres
Request body:
{
"data": {
"type": "genre",
"attributes": {
"name": "New Genre"
}
}
}
Response:
{
"data": {
"id":26,
"type":"genre",
"attributes": {
"name":"New Genre"
}
},
"links": {
"self":"http://api/v1/jad/genres"
}
}
Note! It is important that if the relationship is a collection of items, the item array has to be initailized before use.
Example:
public function __construct()
{
$this->tracks = new ArrayCollection();
}
Create a new playlist with tracks
POST /api/v1/jad/playlists
Request body:
{
"data": {
"type": "playlist",
"attributes": {
"name": "New Playlist"
},
"relationships": {
"tracks": {
"data": [
{ "type": "track", "id": 15 },
{ "type": "track", "id": 43 },
{ "type": "track", "id": 77 },
{ "type": "track", "id": 117 },
{ "type": "track", "id": 351 }
]
}
}
}
}
Response:
{
"data":{
"id":4,
"type":"playlist",
"attributes":{
"name":"New Playlist"
},
"relationships":{
"tracks":{
"links":{
"self":"http://api/v1/jad/playlist/4/relationship/tracks",
"related":"http://api/v1/jad/playlist/4/tracks"
}
}
}
},
"links":{
"self":"http://api/v1/jad/playlist"
}
}
Verifying that the tracks actually have been added to the playlist:
GET /api/v1/jad/playlist/4/relationship/tracks
Response:
{
"data":[
{
"id":15,
"type":"track"
},
{
"id":43,
"type":"track"
},
{
"id":77,
"type":"track"
},
{
"id":117,
"type":"track"
},
{
"id":351,
"type":"track"
}
],
"links":{
"self":"/api/v1/jad/playlist/4/relationship/tracks"
}
}