2.2. 了解 JSON 响应格式
调用 API 返回的结果为 JSON 格式。API 调用返回单选项响应或响应集合的结果。
单个对象的 JSON 响应格式
您可以使用单对象 JSON 响应来处理单个对象。对单个对象的 API 请求需要对象的唯一标识符 :id。
这是 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"
}Collections 的 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。 -
Page- 页面号。 -
per_page- 每页返回的最大对象数量。 -
limit- 在集合响应中返回的指定对象数量。 -
offset- 返回集合前跳过的对象数量。 -
search- 基于scoped_scoped语法的搜索字符串。 排序-
by- 按 API 对集合进行排序的字段指定。 -
order- 排序顺序,ASC 表示升序,或 DESC 代表降序排列。
-
-
results- 对象集合。