使用 REST API
如何将 API 与各种工具(尤其是cURL)
| Redis 云 |
|---|
您可以访问和使用 API 端点 URI (https://api.redislabs.com/v1) 替换为以下任一工具:
- Swagger 用户界面
- cURL HTTP 客户端
- 任何编程语言的 HTTP 客户端
Swagger 用户界面
Swagger UI 对于初始介绍和了解 API作、模型和模拟用法非常有用。
对 Swagger 进行身份验证
要对 Swagger UI 进行身份验证,请执行以下作:
-
在浏览器中打开 Swagger UI 页面。
-
选择
Authorize.Available Authorizations (可用授权) 框显示用于在与 Swagger 的所有 API 交互中进行身份验证的标头和值。
-
插入 API 密钥值:
- 输入 Account Key 作为
x-api-key值,然后选择 Authorize (授权)。 - 输入 Secret Key 作为
x-api-secret-key值,然后选择 Authorize (授权)。 - 选择 Close。
- 输入 Account Key 作为
授权成功后,锁图标会显示一个已关闭的锁。
发出 API 请求
在 Swagger UI 中完成授权后,您可以发出 API 请求:
-
打开作类别并选择一个 API作。
例如,在
Accountcategory 选择GET /payment-methods操作。
-
选择 Try it out (试用),然后选择 Execute (执行)。
API 响应显示在 API作的 Responses (响应) 部分中。 结果包括一个示例,说明如何使用
cURL.
Swagger 中的作输入
某些 API作需要输入,例如:
POST和PUT操作。您应该修改这些示例以满足您的特定需求和账户设置。如果按原样使用,这些示例将失败。有关演示如何使用特定终端节点的更多示例,请参阅 REST API 示例。
使用cURLHTTP 客户端
cURL是一种流行的命令行工具,用于执行 HTTP 请求,
作为单个命令或在 shell 脚本(例如 bash 和 zsh)中。
有关简介,请参阅如何开始使用 cURL 及原因:动手简介。
cURL和 Linux shell 脚本来演示 API;您可以使用任何标准的 REST 客户端或库。我们的示例还使用
jq、JSON 解析器。使用包管理器进行安装(示例:sudo apt install jq)例如,用于获取系统日志信息的标准 API 调用如下所示cURL:
curl -s -X GET "https://$HOST/logs" \
-H "accept: application/json" \
-H "x-api-key: $ACCOUNT_KEY" \
-H "x-api-secret-key: $SECRET_KEY" \
| jq -r .
-
The example expects several variables to be set in the Linux shell:
- $HOST - The URI of the REST API host (
api.redislabs.com/v1) - $ACCOUNT_KEY - The account key value
- $SECRET_KEY - The personal secret key value
- $HOST - The URI of the REST API host (
-
The line "
| jq -r ." means that the HTTP response will be piped (forwarded) to thejqJSON parser, and it will display only the raw output ("-r") of the root element (".") -
You can set the variables using shell commands like the following:
export HOST=api.redislabs.com/v1
export ACCOUNT_KEY={replace-with-your-account-key}
export SECRET_KEY={replace-with-your-secret-key}
On this page