2.2. 了解 JSON 响应格式
对 API 的调用会以 JSON 格式返回结果。API 调用返回单个选项响应或响应集合的结果。
注意
以下示例请求使用 python3
来格式化 Satellite 服务器中的 respone。在 RHEL 7 和一些较旧的系统中,您必须使用 python
而不是 python3
。
单个对象的 JSON 响应格式
您可以使用单对象 JSON 响应来处理单个对象。对单个对象的 API 请求需要对象的唯一标识符 :id
。
这是 ID 为 23 的单对象请求的单对象请求的格式示例:
请求示例:
curl --request GET --insecure --user sat_username:sat_password \ https://satellite.example.com/api/domains/23 | python3 -m json.tool
$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/domains/23 | python3 -m json.tool
响应示例:
集合的 JSON 响应格式
集合是主机和域等对象列表。集合 JSON 响应的格式由 metadata 字段和 results 部分组成。
这是 Satellite 域列表集合请求的格式示例:
请求示例:
curl --request GET --insecure --user sat_username:sat_password \ https://satellite.example.com/api/domains | python3 -m json.tool
$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/domains | python3 -m json.tool
响应示例:
响应元数据字段
API 响应使用以下 metadata 字段:
-
total
- 没有搜索参数的对象总数。 -
Subtotal
- 使用给定搜索参数返回的对象数量。如果没有搜索,则 subtotal 等于 total。 -
页面
- 页面编号。 -
per_page
- 每个页面返回的最大对象数。 -
limit
- 在集合响应中返回的指定数量的对象。 -
offset
- 返回集合前跳过的对象数量。 -
search
- 基于scoped_scoped
语法的搜索字符串。 sort
-
by
- 根据 API 对集合进行排序的字段指定。 -
order
- 排序顺序,ASC 代表升序或 DESC 用于降序。
-
-
results
- 对象的集合。