API网关 APIG-curl:调用API示例

时间:2024-06-27 11:05:53

调用API示例

  1. 使用JavaScript SDK生成curl命令。

    请登录API网关控制台,参考《API网关用户指南》的“SDK”章节,进入SDK页面并下载SDK。

    或直接下载JavaScript SDK的最新版本,并解压。在浏览器中打开demo.html,页面如下图所示。

  2. 填入Key、Secret、方法名、请求协议、 域名 和url。

    1
    2
    3
    4
    5
    6
    // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables.
    // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
    Key=4f5f****100c
    Secret=******
    Method=POST
    Url=https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com
    

  3. 填入json格式的Query和Headers,填入Body。
  4. 单击“Send request”,生成curl命令。将curl命令复制到命令行,访问API。

    $ curl -X POST "https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/" -H "X-Sdk-Date: 20180530T115847Z" -H "Authorization: SDK-HMAC-SHA256 Access=071fe245-9cf6-4d75-822d-c29945a1e06a, SignedHeaders=host;x-sdk-date, Signature=9e5314bd156d517******dd3e5765fdde4" -d ""
    Congratulations, sdk demo is running

    SDK生成的curl命令不符合Window下cmd终端格式,请在git bash下执行生成的curl命令。

support.huaweicloud.com/devg-apig/apig-dev-180525061.html