2.2. 了解 JSON 响应格式
调用 API 返回的结果,采用 JSON 格式。API 调用返回单选响应或响应集合的结果。
单一对象的 JSON 响应格式
您可以使用单一对象 JSON 响应来操作单个对象。对单个对象的 API 请求需要对象的唯一标识符 :id。
这是 Satellite 域的单对象请求格式的示例,ID 为 23:
请求示例:
$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/domains/23 | python -m json.tool
响应示例:
{
"id": 23,
"name": "qa.lab.example.com",
"fullname": "QA",
"dns_id": 10,
"created_at": "2013-08-13T09:02:31Z",
"updated_at": "2013-08-13T09:02:31Z"
}
集合的 JSON 响应格式
集合是主机和域等对象列表。集合 JSON 响应的格式由 metadata 字段和 results 部分组成。
以下是 Satellite 域列表的集合请求格式的示例:
请求示例:
$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/domains | python -m json.tool
响应示例:
{
"total": 3,
"subtotal": 3,
"page": 1,
"per_page": 20,
"search": null,
"sort": {
"by": null,
"order": null
},
"results": [
{
"id": 23,
"name": "qa.lab.example.com",
"fullname": "QA",
"dns_id": 10,
"created_at": "2013-08-13T09:02:31Z",
"updated_at": "2013-08-13T09:02:31Z"
},
{
"id": 25,
"name": "sat.lab.example.com",
"fullname": "SATLAB",
"dns_id": 8,
"created_at": "2013-08-13T08:32:48Z",
"updated_at": "2013-08-14T07:04:03Z"
},
{
"id": 32,
"name": "hr.lab.example.com",
"fullname": "HR",
"dns_id": 8,
"created_at": "2013-08-16T08:32:48Z",
"updated_at": "2013-08-16T07:04:03Z"
}
]
}
响应元数据字段
API 响应使用以下元数据字段:
-
total- 没有搜索参数的对象总数。 -
subtotal- 返回给定搜索参数的对象数量。如果没有搜索,则 subtotal 等于 total。 -
页面- 页面号。 -
per_page- 每个页面返回的最大对象数量。 -
limit- 在集合响应中返回指定的对象数量。 -
offset- 返回集合前跳过的对象数量。 -
search- 基于scoped_scoped语法的搜索字符串。 sort-
by- 通过什么字段指定 API 对集合进行排序。 -
order- 排序顺序,即 ASC 升序,或 DESC 表示降序。
-
-
结果- 对象的集合。