问题中所述的方法实际上意味着对于每个组合ids
有一个资源。假设我们有 2ids
:1和2。
/items/1,2
/items/2,1
上面代表了不同的资源,尽管结果是相同的。
这可能会让 API 的使用者感到困惑。
另一种建模方法是通过查询参数作为过滤语义。
让我们假设,id
实际上是资源的一个字段。
例如,得到item
by id
1:
GET
/items/1
Response:
{
"id": 1,
"type": "table",
"color": "black",
...
}
那么问题来了,如果我需要批量购买几件商品怎么办?
您可以将这个问题概括为过滤的一般问题items
通过某些字段上的值。
例如:获取所有items
类型的table
GET
/items?query="name='table'"
Response:
{
"data": [
{
"id": 1,
"type": "table",
"color": "black",
...
},
{
"id": 2,
"type": "table",
"color": "grey",
...
},
{
"id": 6,
"type": "table",
"color": "brown",
...
}
]
}
所以可以问同样的问题来获得items
where id
is 1
or 2
。
假设我们建模or操作为||
在语法中query
GET
/items?query="id=1||id=2"
Response:
{
"data": [
{
"id": 1,
"type": "table",
"color": "black",
...
},
{
"id": 2,
"type": "table",
"color": "grey",
...
}
]
}