Updated on 2025-05-29 GMT+08:00

Making an API Request

This section describes the structure of a REST API request, and uses the ESM API for creating a query task as an example to describe how to call an API.

Request URI

A request URI is in the following format:

{URI-scheme}://{Endpoint}/{resource-path}?{query-string}

Although a request URI is included in the request header, most programming languages or frameworks require passing the request URI separately.

Table 1 URI parameter description

Parameter

Description

URI-scheme

Protocol used to transmit requests. All APIs use HTTPS.

Endpoint

Domain name or IP address of the server where the REST service is deployed. The value varies depending on services and regions. Obtain the value from Regions and Endpoints.

For example, the endpoint of ESM in the CN South-Guangzhou region is esm-api.cn-south-1.myhuaweicloud.com.

resource-path

Access path of an API. Obtain the path from the URI of an API. For example, the resource-path of the API for creating a query task is /meter/v1/{domain_id}/query-jobs.

query-string

(Optional) Query parameter. It is not necessary to configure query parameters for all APIs. Ensure that there is a question mark (?) before each query parameter, in the format of "Parameter name=Parameter value". For example, ?limit=10 indicates that a maximum of 10 data records will be displayed.

For example, to obtain an ESM token in the CN South-Guangzhou region, obtain the ESM endpoint (esm-api.cn-south-1.myhuaweicloud.com) for this region and the resource-path (/meter/v1/{domain_id}/query-jobs) in the URI of the API used to create a query task. Then, construct the URI as follows:
https://esm-api.cn-south-1.myhuaweicloud.com/meter/v1/{domain_id}/query-jobs
Figure 1 Example URI

To simplify the URI display, this document provides only the resource-path and request method in the URI of each API. The URI-scheme of all APIs is HTTPS, and the endpoints of all APIs in the same region are identical.

Request Methods

The HTTP protocol defines the following request methods that can be used to send a request to the server.

Table 2 HTTP methods

Method

Description

GET

Requests the server to return specified resources.

PUT

Requests the server to update specified resources.

POST

Requests the server to add resources or perform special operations.

DELETE

Request the server to delete specified resources, for example, an object.

For example, in the URI of the API used to create a query task, the request method is POST. The request is as follows:

POST https://esm-api.cn-south-1.myhuaweicloud.com/meter/v1/{domain_id}/query-jobs

Request Headers

You can add additional header fields to a request, such as the fields required by a specified URI or HTTP method. For example, to request for the authentication information, add Content-Type, which specifies the request body type.

Table 3 lists common request header fields.

Table 3 Common request header fields

Name

Description

Mandatory

Example

Content-Type

Type (or format) of the request body being sent from a client to a server. The default value application/json is recommended. Other values will be described in specific APIs.

Yes

application/json

Accept

Method of receiving the response body. Currently, the value can only be application/json.

No

application/json

X-Auth-Token

User token.

It is a response to the API for obtaining a user token. This API is the only one that does not require authentication.

After a response is returned, the value of X-Subject-Token in the response header is the token value.

Yes

This field is mandatory for authentication using tokens.

The following is part of an example token:

MIIPAgYJKoZIhvcNAQcCo...ggg1BBIINPXsidG9rZ

(Optional) Request Body

This part is optional. A request body is generally sent in a structured format (for example, JSON or XML), which is specified by Content-Type in the request header. It is used to transfer content other than the request header. If the request body contains full-width characters, these characters must be coded in UTF-8.

Request bodies vary depending on APIs. Some APIs do not require a request body, such as the APIs that need to be requested using GET and DELETE methods.

For the API used to create a query task, you can obtain the required request parameters and parameter description from the API request. The following is a request carrying a request body, where values of fields are examples:

POST https://esm-api.cn-south-1.myhuaweicloud.com/meter/v1/{domain_id}/query-jobs
Content-Type: application/json 

{
  "hcso_id": "********",
  "region_id": "********",
  "min_sdr_time": "2022-01-01 00:00:00",
  "max_sdr_time": "2022-02-01 00:00:00"
}

If all data required for the API request is available, you can send a request to call an API through curl, Postman, or coding. In the response to the API used to obtain a user token, x-subject-token is the target user token. This token can then be used to authenticate other API calls.