Updated on 2024-01-26 GMT+08:00

curl

Scenarios

To use the curl command to call an API through App authentication, download the JavaScript SDK to generate the curl command, and copy the command to the CLI to call the API.

Prerequisites

  • You have obtained API calling information. For details, see Preparations.
  • The browser must be Chrome 89.0 or later.

Obtaining the SDK

Old version: Log in to the ROMA Connect console, choose API Connect > API Calling > SDKs, and download the SDK.

New version: Log in to the ROMA Connect console, choose API Connect > Credentials > SDKs, and download the SDK.

The following shows the directory structure after the decompression.

Name

Description

signer.js

SDK code

node_demo.js

Node.js sample code

demo.html

Browser sample code

demo_require.html

Browser sample code (loaded using require)

test.js

Test cases

js\hmac-sha256.js

Dependencies

licenses\license-crypto-js

Third-party licenses

licenses\license-node

API Calling Example

  1. Use the JavaScript SDK to generate the curl command.

    Decompress the SDK. Open demo.html in a browser. The following figure shows the demo page.

  2. Enter the key and secret of the authorized credential, and specify the method name and request URL. For details, see Obtaining API Calling Information. Example:
    // 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=4f5f626b-073f-402f-a1e0-e52171c6100c
    Secret=******
    Method=POST
    Url=https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/app1
  3. Enter query and header parameters in JSON format, and set the request body.
  4. Click Send request to generate a curl command. Copy the curl command to the CLI to access the API.
    //If the subdomain name allocated by the system is used to access the API of HTTPS requests, add -k to the end of -d to ignore the certificate verification. 
    $ curl -X POST "https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/app1" -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

    The curl command generated using an SDK does not meet the format requirements of Windows. Please run the curl command in Git Bash.