Access via SDK/API
For application scenarios that solely use cloud hosting as an API server, access to the cloud hosting service can be achieved via open SDK/API. Similar to the built-in callContainer method in WeChat Mini Programs, accessing via open SDK/API does not require enabling public network access. By accessing the cloud hosting service through open SDK/API, fine-grained permission control can be implemented based on access_token permission control.
If it is a web application, you need to configure the security domain first to support CORS cross-origin requests.
JS-SDK Access
import cloudbase from "@cloudbase/js-sdk";
//Initialize the SDK instance
const app = cloudbase.init({
env: "xxxx-yyy",
});
app
.callContainer({
name: "helloworld",
method: "POST",
path: "/abc",
header: {
"Content-Type": "application/json; charset=utf-8",
},
data: {
key1: "test value 1",
key2: "test value 2",
},
})
.then((res) => {
console.log(res);
});
For more information, refer to the JS-SDK documentation.
Node.js SDK Access
As in accessing Cloud Hosting Service in Cloud Function:
import tcb from '@cloudbase/node-sdk'
exports.main = async (event, context) => {
const { httpContext } = context
const { url, httpMethod } = httpContext
console.log(`[${httpMethod}][${url}]`)
const tcbapp = tcb.init({ context })
const result = await tcbapp.callContainer({
name: 'helloworld',
method: 'POST',
path: '/abc',
data: {
key1: 'test value 1',
key2: 'test value 2'
},
{
timeout: 5000
}
})
console.log(result)
}
For more information, refer to the Node.js SDK documentation.
HTTP API Access
To access the cloud hosting service via the Cloud Hosting HTTP API, you need to first obtain an access token (Token) and then use that token to make API calls.
curl -L 'https://your-envId.api.tcloudbasegateway.com/v1/cloudrun/:name' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <TOKEN>' \
-d '{}'
To address potential compliance blocking risks, the HTTP API encapsulates both requests and responses:
- All requests must carry an access token in the format
Authorization: Bearer <TOKEN>
; - For all responses to
GET
requests, addContent-Disposition: attachment
to the response headers; For non-text types (text types include responses with theContent-Type
header indicatingapplication/json
,application/x-www-form-urlencoded
, ortext/plain
), addContent-Disposition: attachment
to the response headers; - Server-sent Event and WebSocket requests are not subject to the above restrictions.