Skip to main content

Access Cloud Functions via HTTP

Through the HTTP Gateway, you can configure cloud functions as standard HTTP interfaces and call them via HTTP requests without an SDK. This document explains how to configure HTTP access for cloud functions and how to handle requests and responses.

Custom Domain

If you need to bind a custom domain to a cloud function (recommended for production), the complete custom domain workflow (add domain, configure SSL certificate, choose CDN, domain ownership verification, DNS resolution, etc.) is documented in Custom Domain Configuration. All domains added through any entry point are listed and managed centrally in the HTTP Gateway.

Prerequisites​

  • CloudBase environment created
  • At least one cloud function deployed

Configure HTTP Access​

Configure the domain, access path, and associated resource in the HTTP Gateway.

  1. Go to the HTTP Gateway
  2. Navigate to Route Management and click "New"
  3. Configure the following information:
Configuration ItemDescriptionExample
DomainSelect default domain, custom domain, or * (matches all domains)Default domain
Trigger PathSet HTTP access path, supports / or custom path/api/hello
Associated Resource TypeSelect "Cloud Function"Cloud Function
Cloud Function InstanceSelect the target cloud functionhello-world
VersionFunction version the route invokes; see "Bind a Specific Version" below$DEFAULT

Tip: For production environments, it's recommended to bind a custom domain to get full service capabilities. Please refer to Custom Domain Configuration for configuration methods.

Bind a Specific Version​

The "Version" dropdown selects the function version this route invokes:

Version OptionDescription
$DEFAULT (follow version traffic configuration)Selected by default. Not bound to a specific version; requests are distributed by the function's version traffic ratios, consistent with current behavior
$LATEST (latest version)Always uses the latest code and takes effect automatically after a new version is published, regardless of traffic ratios
Version N (specific published version)The route is fixed to that version's snapshot; later code updates do not affect this route

The two preset options are always pinned at the top, followed by published versions in descending order with their release dates.

Test Access​

After configuration, access domain + trigger path to invoke the cloud function:

# Example: Access cloud function via default domain
curl https://your-env-id.<your-env-region>.app.tcloudbase.com/api/hello

Make HTTP Requests​

After configuration, you can use any HTTP client to access cloud functions.

# GET request
curl https://your-domain/your-function-path

# POST request
curl -X POST https://your-domain/your-function-path \
-H "Content-Type: application/json" \
-d '{"name": "CloudBase", "version": "1.0"}'

Handle HTTP Requests in Cloud Functions​

Receive Request Information​

When accessing a cloud function via HTTP, the function's event parameter will contain complete HTTP request information:

exports.main = async (event, context) => {
// event object structure
const {
path, // HTTP request path, e.g. /api/hello
httpMethod, // HTTP request method, e.g. GET, POST, PUT, DELETE
headers, // HTTP request headers object
queryStringParameters, // URL query parameters object, e.g. ?name=value
body, // HTTP request body content (string format)
isBase64Encoded, // Whether body is Base64 encoded
requestContext // CloudBase environment related information
} = event;

// Return response
return { message: 'success' };
};

Example: Handle Different Types of Requests​

exports.main = async (event) => {
const { queryStringParameters } = event;

// Get query parameters
const name = queryStringParameters?.name || 'Guest';

return {
statusCode: 200,
body: JSON.stringify({
message: `Hello, ${name}!`,
timestamp: Date.now()
})
};
};

Access Example:

curl "https://your-domain/greet?name=CloudBase"
# Response: {"message":"Hello, CloudBase!","timestamp":1699999999999}