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

Step 1: Create Domain Association

  1. Go to CloudBase Console - HTTP Access Service
  2. Click the "New" button 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.service.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}

Return HTTP Response

Cloud functions support two response methods: simple response and integrated response. The system automatically recognizes the response type based on the return value format.

Simple Response

Directly return data, and the system automatically generates a standard HTTP response.

exports.main = async () => {
return "Hello CloudBase";
};

HTTP Response:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8

Hello CloudBase

Integrated Response (Advanced)

When you need to precisely control HTTP status codes, response headers, etc., use the integrated response format:

{
statusCode: number, // HTTP status code (required)
headers: { // HTTP response headers (optional)
"headerName": "headerValue"
},
body: string, // Response body content (optional)
isBase64Encoded: boolean // Whether body is Base64 encoded (optional)
}

💡 Recognition Rule: When the return value contains the statusCode field, the system will recognize it as an integrated response.

Integrated Response Examples

exports.main = async () => {
return {
statusCode: 200,
headers: {
"Content-Type": "text/html; charset=utf-8"
},
body: `
<!DOCTYPE html>
<html>
<head><title>CloudBase</title></head>
<body>
<h1>Welcome to CloudBase</h1>
<p>This is an HTML page returned via cloud function</p>
</body>
</html>
`
};
};

The browser will render this HTML page directly when accessed.