Access via SDK/API
For application scenarios that solely use CloudRun as an API server, the CloudRun service can be accessed by exposing SDK/API. Similar to the built-in callContainer method in WeChat Mini Programs, accessing via exposed SDK/API does not require enabling public network access to access the CloudRun service. By accessing the CloudRun service via exposed SDK/API, fine-grained permission control can be implemented based on access_token for service access.
For a web application, secure domain configuration is required first to support CORS cross-domain 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, please refer to the JS-SDK documentation.
Node SDK Access
As in accessing the CloudRun 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, please refer to the Node SDK documentation.
HTTP API Access
To access the CloudRun service via the CloudRun HTTP API, you need to first obtain an access token (Token), then use this token for 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
GETrequests, addContent-Disposition: attachmentto the response headers; - For responses of non-text types (text types include return content where the response header
Content-Typehas values such asapplication/json,application/x-www-form-urlencoded,text/plain), addContent-Disposition: attachmentto the response headers; - Server-sent Event and WebSocket requests are not subject to the above restrictions.