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.
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.
- Go to the HTTP Gateway
- Navigate to Route Management and click "New"
- Configure the following information:
| Configuration Item | Description | Example |
|---|---|---|
| Domain | Select default domain, custom domain, or * (matches all domains) | Default domain |
| Trigger Path | Set HTTP access path, supports / or custom path | /api/hello |
| Associated Resource Type | Select "Cloud Function" | Cloud Function |
| Cloud Function Instance | Select the target cloud function | hello-world |
| Version | Function 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 Option | Description |
|---|---|
$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.
- curl
- JavaScript
- Python
# 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"}'
// Using fetch API
const response = await fetch('https://your-domain/your-function-path', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: 'CloudBase', version: '1.0' })
});
const data = await response.json();
console.log(data);
import requests
# POST request
response = requests.post(
'https://your-domain/your-function-path',
json={'name': 'CloudBase', 'version': '1.0'}
)
data = response.json()
print(data)
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
- GET Request
- POST Request
- RESTful API
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}
exports.main = async (event) => {
const { body } = event;
// Parse JSON request body
const data = JSON.parse(body);
return {
statusCode: 200,
body: JSON.stringify({
received: data,
processed: true
})
};
};
Access Example:
curl -X POST https://your-domain/process \
-H "Content-Type: application/json" \
-d '{"userId": 123, "action": "update"}'
exports.main = async (event) => {
const { httpMethod, path, queryStringParameters, body } = event;
// Route handling
if (path === '/users' && httpMethod === 'GET') {
// Get user list
return {
statusCode: 200,
body: JSON.stringify([{ id: 1, name: 'User1' }])
};
}
if (path.startsWith('/users/') && httpMethod === 'GET') {
// Get single user
const userId = path.split('/')[2];
return {
statusCode: 200,
body: JSON.stringify({ id: userId, name: 'User' })
};
}
// 404
return {
statusCode: 404,
body: JSON.stringify({ error: 'Not Found' })
};
};
Related Documentation
- HTTP Gateway Overview - Understand the core features and advantages of HTTP Gateway
- Quick Start - Quick start guide
- Route Configuration - Configure flexible routing rules
- Custom Domain Configuration - Bind custom domains
- Cloud Function Development Guide - Cloud function development tutorial