Updated on 2024-03-22 GMT+08:00

Making an API Request

This section describes the structure of a REST API request and demonstrates 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 and frameworks require the request URI to be transmitted 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 bearing the REST service endpoint. The endpoint varies depending on services in different regions. For more details, see Endpoints.

resource-path

Path in which the requested resource is located, that is, the API access path.

query-string

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

For example, to call the API in the AP-Bangkok region, use the endpoint ocr.ap-southeast-2.myhuaweicloud.com of the AP-Bangkok region and find the value /v3/auth/tokens of resource-path in Obtaining a User Token. The URI is as follows:

https://ocr.ap-southeast-2.myhuaweicloud.com/v2/{project_id}/ocr/web-image
Figure 1 Example URI

To simplify the URI display in this document, each API is provided only with a resource-path and a request method. 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-defined request 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

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

HEAD

Same as GET except that the server must return only the response header.

PATCH

Requests the server to update partial content of a specified resource.

If the resource does not exist, a new resource will be created.

POST https://ocr.ap-southeast-2.myhuaweicloud.com/v2/{project_id}/ocr/web-image

Request Header

You can also 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.

  • Content-Type: 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: user token. This field is required only for token-based API authentication. For details about the user token, see "Authentication".

The following provides an example request with a request header included.

POST https://ocr.ap-southeast-2.myhuaweicloud.com/v2/{project_id}/ocr/web-image
Content-Type: application/json
x-auth-token: MIIaBgYJKoZIhvcNAQcC...

Request Body

The body of a request is often sent in a structured format as specified in the Content-Type header field. The request body transfers all content except the request header.

The request body varies depending on APIs. Some APIs do not require the request body, for example, the APIs requested using the GET and DELETE methods.

The request parameters and parameter description are available in the request. The following provides an example request with a body included.

POST https://ocr.ap-southeast-2.myhuaweicloud.com/v2/{project_id}/ocr/web-image
Request Header:   
Content-Type: application/json   
X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
Request Body:
{
     "image":"/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAAgABwESAAMAA..."
}

If all data required for the API request is available, you can send the request to call the API through curl, Postman, or coding.