Updated on 2024-02-21 GMT+08:00

Making an API Request

This section describes the structure of a REST API, and uses the IAM API for obtaining a user token as an example to demonstrate how to call an API. The obtained token can then be used to authenticate the calling of other APIs.

Request URI

The format of a request URI is as follows:

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

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

Table 1 Parameters in a URI

Parameter

Description

URI-scheme

Protocol used to transmit requests. All APIs use HTTPS.

Endpoint

Domain name or IP address of the server bearing the REST service endpoint. The endpoint varies between services in different regions.

Obtain the value of this parameter from Regions and Endpoints.

resource-path

Resource path, that is, API access path. Obtain the value from the URI of the API. For example, the resource-path of the API used to obtain a user token is /v3/auth/tokens.

query-string

Query parameter, which is optional. Ensure that a question mark (?) is included in front of each query parameter that is in the format of Parameter name=Parameter value. For example, ? limit=10 indicates that a maximum of 10 data records will be queried.

For example, to obtain an IAM token in the CN-Hong Kong region, use the endpoint of IAM (iam.ap-southeast-1.myhuaweicloud.com) for this region and resource-path (/v3/auth/tokens) in the URI of the API used to obtain a user token. Then, construct the URI as follows:

https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens

To simplify the URI display, each API is provided only with resource-path and a request method. This is because the URI-scheme value of all APIs is HTTPS, and the endpoints in a region are the same.

Request Method

HTTP-based request methods specify the type of operations that you are requesting.

Table 2 HTTP methods

Method

Description

GET

Requests a 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

Requests the server to delete specific resources, for example, an object.

HEAD

Requests the server resource header.

PATCH

Requests the server to update part of specified resources.

If the resource does not exist, the PATCH method may create a resource.

For example, in the case of the API used to obtain a user token, the request method is POST. The request is as follows:

POST https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens

Request Header

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

Common request header fields are as follows:

  • Content-Type: specifies the request body type or format. This field is mandatory and its default value is application/json. Other values of this field will be provided for specific APIs if any.
  • X-Auth-Token: specifies a user token, which is optional. This field is mandatory when token authentication is used. The user token is a response to the API used to obtain a user token. This API is the only one that does not require authentication.

    In addition to supporting token-based authentication, APIs also support authentication using access key ID/secret access key (AK/SK). During AK/SK-based authentication, an SDK is used to sign the request, and the Authorization (signature authentication) and X-Sdk-Date (time when the request is sent) header fields are automatically added to the request.

    For more details, see Authentication Using AK/SK.

  • X-Project-ID: subproject ID. This field is optional and can be used in multi-project scenarios.
  • X-Domain-ID: account ID.

The API used to obtain a user token does not require authentication. Therefore, only the Content-Type field needs to be added to requests for calling the API. An example of such requests is as follows:

POST https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens
Content-Type: application/json

Request Body

This part is optional. The body of a request is often sent in a structured format as specified in Content-Type. The request body transfers content except the request header. If the request body contains Chinese characters, these characters must be coded in UTF-8.

Request bodies vary with APIs. Some APIs do not require a request body, such as the APIs requested using GET and DELETE methods.

In the case of the API used to obtain a user token, the request parameters and parameter description can be obtained from the API request. The following provides an example request with a body included. Replace username, domainname, ******** (login password), and xxxxxxxxxxxxxxxxxx (project name, for example, cn-north-1) with the actual values. Obtain a project name from Regions and Endpoints.

The scope parameter specifies where a token takes effect. You can set scope to an account or a project under an account. In the following example, the token takes effect only for the resources in a specified project. For more information about this API, see Obtaining a User Token.

POST https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens
Content-Type: application/json

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "name": "username",
                    "password": "********",
                    "domain": {
                        "name": "domainname"
                    }
                }
            }
        },
        "scope": {
            "project": {
                "id": "xxxxxxxx"
            }
        }
    }
}

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