云托管
callContainer
1. 接口描述
接口功能:调用云托管服务
接口声明:callContainer<ParaT, ResultT>(callContainerOptions: ICallContainerOptions<ParaT>, opts?: ICustomReqOpts): Promise<CallContainerResult<ResultT>>
2. 输入参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
callContainerOptions | ICallContainerOptions<ParaT> | 是 | 云托管调用请求参数 |
opts | ICustomReqOpts | 否 | 自定义配置,目前支持 SDK 请求超时时间设置,{timeout: number} |
ICallContainerOptions<ParaT>
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
name | string | 是 | 云托管服务名 |
method | string | 否 | HTTP 请求方法 |
path | string | 否 | HTTP 请求路径 |
header | Record<string, string> | 否 | HTTP 请求头 |
data | object | 否 | HTTP 请求体 |
3. 返回结果
Promise<CallContainerResult<ResultT>>
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
requestId | string | 否 | RequestId,用于错误排查 |
statusCode | number | 否 | HTTP 响应状态码,用于错误排查 |
header | Record<string, string> | 否 | HTTP 响应头,用于错误排查 |
data | ResultT | 否 | 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);
};