Help Center/ Koo Command Line Interface/ User Guide/ Options/ Passing API Parameters with JSON File
Updated on 2024-06-17 GMT+08:00

Passing API Parameters with JSON File

When calling a cloud service API that has too many parameters, use --cli-jsonInput to input some or all parameters of the API through a JSON file rather than directly entering the parameters in the command. Enter other parameters, such as KooCLI system parameters and API parameters that are not included in the JSON file, in the command. Example:

hcloud ECS CreateServers --cli-region="ap-southeast-1" --cli-read-timeout=60 --cli-jsonInput=C:\cli\Ecs_CreateServers.json
{
    "job_id": "ff808082******************ae0646",
    "serverIds": [
        "dd86****-****-****-****-****91527651"
    ]
}

To pass API parameters through --cli-jsonInput, compile a JSON file and place each API parameter in the corresponding key based on its location in the request.

You can construct a JSON file using either of the following methods:

Method 1

Use --skeleton to generate and fill an API input parameter skeleton file in JSON format, and pass the file using --cli-jsonInput=${JSON file name}.

Method 2

Obtain API parameters in API Explorer and write them into a JSON file.

  1. Add --help to the end of the original command and run the command. Then view the location of each API parameter in Params of the command output.
  2. Create a JSON file and name it in the format ${Service name}_${API name}.json. Add the following content to the file:

    {
        "header": {},
        "path": {},
        "query": {},
        "formData": {},
        "cookie": {},
        "body": {}
    }

  3. Set parameters based on the key order in the JSON file.

    • For non-body parameters, add parameter-value pairs in the relevant curly brackets ({}) and separate them with commas (,). Do not add any commas between the last pair and the right bracket (}).
    • For body parameters, set them in API Explorer and then click Edit Code, as shown in the following figure. Copy the JSON format parameters to the body key in the JSON file while keeping only one pair of brackets.
    Figure 1 Obtaining body parameters for cli-jsonInput

  4. If there is no parameter next to a key, delete the entire row where the key locates. When deleting the last key, delete the comma (,) between the outermost and previous right brackets (}).
  5. In your KooCLI command, use --cli-jsonInput=${JSON file location} to pass API parameters, and run the command.

    In the preceding example, the Ecs_CreateServers.json file passed by --cli-jsonInput=C:\cli\Ecs_CreateServers.json should be as follows:

    {
        "path": {
            "project_id": "0dd8cb41******************a84546"
        },
        "body": {
            "server": {
                "adminPass": "wh***********",
                "auto_terminate_time": "2022-01-19T03:30:52Z",
                "availability_zone": "ap-southeast-1a",
                "data_volumes": [
                    {
                        "multiattach": true,
                        "shareable": true,
                        "size": 100,
                        "volumetype": "SATA"
                    }
                ],
                "flavorRef": "2d53****-****-****-****-****257bf163",
                "imageRef": "7059****-****-****-****-****0b5e9e4c",
                "name": "ecs_server_01",
                "nics": [
                    {
                        "ipv6_enable": true,
                        "subnet_id": "4eb2****-****-****-****-****ff9a042d"
                    }
                ],
                "publicip": {
                    "eip": {
                        "bandwidth": {
                            "sharetype": "PER",
                            "size": 30
                        },
                        "iptype": "5_sbgp"
                    }
                },
                "root_volume": {
                    "volumetype": "SATA"
                },
                "server_tags": [
                    {
                        "key": "date",
                        "value": "211102"
                    }
                ],
                "vpcid": "5aa5****-****-****-****-****1df05a3a"
            }
        }
    }

For more precautions, see FAQs.