我一直在阅读 jsonapi 的文档,但我无法理解这是如何实用的。根据向文章添加评论的文档,评论必须已经存在。
POST /articles/1/relationships/comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": [
{ "type": "comments", "id": "123" }
]
}
这只是一个糟糕的例子,还是规范真的希望您在发出上述请求以将其关联到总共 2 个请求之前发出创建与实体无关的评论的请求?
您似乎更有可能希望发出这样的请求:
POST /comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "comments",
"attributes": {
"body": "blah blah blah"
},
"relationships": {
"article": {
"data": { "type": "articles", "id": "45" }
}
}
}
}
或者更好:
POST /articles/45/relationships/comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": [
{
"type": "comments",
"attributes": {
"body": "blah blah blah"
}
}
]
}