跳到主要内容

云托管

callContainer​

1. 接口描述​

接口功能:调用云托管服务

接口声明:callContainer<ParaT, ResultT>(callContainerOptions: ICallContainerOptions<ParaT>, opts?: ICustomReqOpts): Promise<CallContainerResult<ResultT>>

2. 输入参数​

字段类型必填说明
callContainerOptionsICallContainerOptions<ParaT>是云托管调用请求参数
optsICustomReqOpts否自定义配置,目前支持 SDK 请求超时时间设置,{timeout: number}

ICallContainerOptions<ParaT>

字段类型必填说明
namestring是云托管服务名
methodstring否HTTP 请求方法
pathstring否HTTP 请求路径
headerRecord<string, string>否HTTP 请求头
dataobject否HTTP 请求体

3. 返回结果​

Promise<CallContainerResult<ResultT>>

字段类型必填说明
requestIdstring否RequestId,用于错误排查
statusCodenumber否HTTP 响应状态码,用于错误排查
headerRecord<string, string>否HTTP 响应头,用于错误排查
dataResultT否HTTP 响应体数据

函数执行报错,将通过异常抛出

4. 示例代码​

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);
};