Skip to main content

Access Cloud Functions via HTTP

Through the HTTP access service, 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.

Prerequisites

  • CloudBase environment created
  • At least one cloud function deployed

Configure HTTP Access

You can bind a domain directly on the cloud function detail page (recommended), or configure it globally in the HTTP access service.

  1. Go to Cloud Functions and open the detail page of the target cloud function
  2. In the "Custom Domain" section, click "Add Domain" to open the binding dialog
  3. Set the trigger path for the HTTP access, supports / or a custom path
  4. Select a custom domain and confirm. All bound domains are listed in the "Domain Management" section of the detail page

Method 2: Bind via the HTTP Access Service

If you prefer to manage domain-resource mappings from a global view, you can also configure them in the HTTP access service:

  1. Go to HTTP Access Service
  2. Click "New" in the "Domain Associated Resources" section
  3. Configure the following information:
Configuration ItemDescriptionExample
Associated Resource TypeSelect "Cloud Function", then choose target cloud functionhello-world
DomainSelect default domain, custom domain, or * (matches all domains)Default domain
Trigger PathSet HTTP access path, supports / or custom path/api/hello

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

Step 2: 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}