Using HTTP to Access Cloud Functions
Developers can invoke cloud functions via HTTP and transform the return value into the HTTP response body. In cloud development, the input and output parameters of cloud functions are generally required to be JSON objects, requiring certain mapping rules to correspond to HTTP requests and responses.
Cross-Origin Handling
HTTP access service natively supports cross-origin requests. Add domains to Security Domains, and web pages under these domains can access the HTTP service cross-origin.
Input Parameters of Cloud Functions
When using HTTP to invoke cloud functions, HTTP requests are transformed into a special structure called Integration Request, as shown below:
{
path: path: 'HTTP request path, such as /hello',
httpMethod: httpMethod: 'HTTP request method, such as GET',
headers: {
// HTTP request headers
},
queryStringParameters: {
// HTTP request Query, in key-value pair format
},
requestContext: {
// Cloud Development related information
},
body: body: 'HTTP request body',
isBase64Encoded: isBase64Encoded: 'true or false, indicating whether the body is Base64 encoded'
}
Below is an example:
{
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: ''
}
Return Value of Cloud Function
Cloud functions can return data of types such as string, object, number, or return an integrated response. The return value will then be transformed into a standard HTTP response.
Returning a String or Number
If the Cloud Function returns a string,
exports.main = async function() {
return "hello gateway";
};
The final HTTP response is:
HTTP/1.1 200 OK
date: Mon, 16 Dec 2019 08:35:31 GMT
content-type: text/plain; charset=utf-8
content-length: 13
hello gateway
Returning an Object
The returned Object will be converted to JSON, and the HTTP response's content-type will be set to application/json):
exports.main = async function() {
return {
foo: "bar",
};
};
The final HTTP response is:
HTTP/1.1 200 OK
date: Mon, 16 Dec 2019 08:35:31 GMT
content-type: application/json; charset=utf-8
content-length: 13
{"foo":"bar"}
Returning an Integrated Response
Cloud functions can return a specially structured Integrated Response as follows to flexibly control the response body:
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
Using Integrated Response to Return HTML
Set content-type to text/html to return HTML in the body, which will be automatically parsed by browsers:
exports.main = async function() {
return {
statusCode: 200,
headers: {
"content-type": "text/html",
},
body: "<h1>Hello</h1>",
};
};
The final HTTP response is:
HTTP/1.1 200 OK
date: Mon, 16 Dec 2019 08:35:31 GMT
content-type: text/html; charset=utf-8
content-length: 14
<h1>Hello</h1>
Using Integrated Response to Return JS Files
Set content-type to application/javascript to return a JavaScript file in the body:
exports.main = async function() {
return {
statusCode: 200,
headers: {
"content-type": "application/javascript",
},
body: 'console.log("Hello!")',
};
};
The final HTTP response is:
HTTP/1.1 200 OK
date: Mon, 16 Dec 2019 08:35:31 GMT
content-type: application/javascript; charset=utf-8
content-length: 21
console.log("Hello!")
Using Integrated Response to Return Binary Files
If the response body is a binary file such as images, audio, or video, you can set isBase64Encoded to true and convert the binary content into a Base64-encoded string, for example:
exports.main = async function() {
return {
isBase64Encoded: true,
statusCode: 200,
headers: {
"content-type": "image/png",
},
body: "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAY...",
};
};
The final HTTP response is a PNG image:
HTTP/1.1 200 OK
date: Mon, 16 Dec 2019 08:35:31 GMT
content-type: image/png
content-length: 9897
<binary payload...>
Implementing 302 Redirects Using Integrated Response
Suppose you want to implement a 302 intermediary proxy for all paths under a specific URL
exports.main = async function(event) {
const {
path
} = event;
return {
statusCode: 302,
headers: {
location: `https://tcb.cloud.tencent.com${path}`,
},
};
};