2.2. 了解 JSON 响应格式
对 API 的调用返回 JSON 格式的结果。API 调用返回单选项响应或响应集合的结果。
注意
以下示例请求使用 python3 来格式化 Satellite 服务器中的 respone。在 RHEL 7 和一些较旧的系统中,您必须使用 python 而不是 python3。
单对象的 JSON 响应格式
您可以使用单对象 JSON 响应来使用单个对象。对单个对象的 API 请求需要对象的唯一标识符 :id。
这是 ID 为 23 的 Satellite 域的单对象请求格式示例:
请求示例:
$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/domains/23 | python3 -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 | python3 -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 (降序)。
-
-
结果- 对象的集合。