Skip to main content

HTTP Access to Cloud Functions

By configuring HTTP Access Service, you can make cloud functions respond to HTTP requests just like regular Web APIs.

How It Works

When you access cloud functions through HTTP requests:

  1. Request Conversion: HTTP requests are automatically converted into a specific JSON format and passed to the cloud function
  2. Response Conversion: The return value of the cloud function is converted into a standard HTTP response and returned to the client

This way, you can call cloud functions using the familiar HTTP approach.

CORS Configuration

HTTP Access Service naturally supports cross-origin requests. Simply add your domain to CloudBase Console/Environment Configuration/Security Domains, and web pages under that domain can normally access your cloud functions.

How Cloud Functions Receive HTTP Requests

Request Parameter Format

When you send an HTTP request to a cloud function, the cloud function receives an object containing all request information, which we call an integrated request:

{
path: 'HTTP request path, e.g., /hello',
httpMethod: 'HTTP request method, e.g., GET, POST',
headers: {
// HTTP request header information
},
queryStringParameters: {
// URL query parameters, e.g., ?name=value
},
requestContext: {
// CloudBase environment related information
},
body: 'HTTP request body content',
isBase64Encoded: 'true/false, indicates whether body is Base64 encoded'
}

Actual Example

Suppose you sent a GET request to https://env-id.service.tcloudbase.com/, the cloud function would receive parameters like this:

{
path: '/',
httpMethod: 'GET',
headers: {
'host': 'env-id.service.tcloudbase.com',
'connection': 'close',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8'
},
requestContext: {
requestId: 'cdbb96328072184d19d3fcd243e8cc4d',
envId: 'env-id',
appId: 123456789,
uin: 123456789
},
isBase64Encoded: false,
body: ''
}

How Cloud Functions Return HTTP Responses

Cloud functions have multiple ways to return data, and the system automatically converts these return values into standard HTTP responses.

Method 1: Directly Return Simple Data

Return String

The simplest way is to directly return a string:

exports.main = async function() {
return "hello gateway";
};

The client will receive this HTTP response:

HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8

hello gateway

Return JSON Object

When returning an object, it will be automatically converted to JSON format:

exports.main = async function() {
return {
foo: "bar",
message: "success"
};
};

The client will receive:

HTTP/1.1 200 OK
content-type: application/json; charset=utf-8

{"foo":"bar","message":"success"}

Method 2: Return Integrated Response (Advanced Usage)

If you need precise control over HTTP response status codes, response headers, and other information, you can return a specially formatted integrated response:

{
"isBase64Encoded": true|false, // Whether body is Base64 encoded
"statusCode": 200, // HTTP status code
"headers": { // Custom response headers
"headerName": "headerValue"
},
"body": "Response content" // Response body
}

Example 1: Return HTML Page

exports.main = async function() {
return {
statusCode: 200,
headers: {
"content-type": "text/html",
},
body: "<h1>Welcome to My Website</h1><p>This is an HTML page returned through cloud functions</p>",
};
};

The browser will directly display this HTML page.

Example 2: Return JavaScript File

exports.main = async function() {
return {
statusCode: 200,
headers: {
"content-type": "application/javascript",
},
body: 'console.log("Hello from Cloud Function!");',
};
};

Example 3: Return Images and Other Binary Files

For images, audio/video, and other binary files, you need to convert the content to Base64 encoding:

exports.main = async function() {
return {
isBase64Encoded: true,
statusCode: 200,
headers: {
"content-type": "image/png",
},
body: "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAY...", // Base64 encoded image data
};
};

The client will receive a complete PNG image.

Example 4: Implement Page Redirect

You can also return a 302 status code to implement page redirection:

exports.main = async function(event) {
const { path } = event;
return {
statusCode: 302,
headers: {
location: `https://tcb.cloud.tencent.com${path}`,
},
};
};

When users access your cloud function, they will be automatically redirected to the corresponding page on the Tencent CloudBase official website.