抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台
  • 快速入门
  • 开发指南
  • 功能服务
  • 行业解决方案
  • 迁移指南
  • API 参考
  • 云函数
  • 云函数服务端 API
  • 云函数客户端 API
  • 云托管
  • 触发器接口开发文档
  • 云数据库
  • 对象存储
  • 媒资管理OpenApi
  • 配置中心SDK
  • 公共错误码
  • 最佳实践
  • 产品计费
  • 常见问题
  • 产品动态
  • 云函数服务端 API

    收藏
    我的收藏
    在本地或者云端新建一个云函数后,统一有两个入参,params 和 context 如下,本文将介绍 params 和 context 的具体含义和如何使用。

    JS 云函数

    /** * @param params 调用参数,HTTP 请求下为请求体 * @param context 调用上下文 * @return 函数的返回数据,HTTP 场景下会作为 Response Body * */ let { dySDK } = require("@open-dy/node-server-sdk"); module.exports = async function (params, context) { };

    TS 云函数

    /** * @param params 调用参数,HTTP 请求下为请求体 * @param context 调用上下文 * @return 函数的返回数据,HTTP 场景下会作为 Response Body * */ import { dySDK } from '@open-dy/node-server-sdk'; export default async function (params: any, context: any) { };

    params

    params 表示 以HTTP 域名方式或者 callContainer 调用云函数时传递的参数,目前支持get post两种形式调用,在调用云函数后云函数内能通过 params 获取具体调用入参,post 形式调用云函数时params内容格式根据请求头中的content-type 而定,详细描述如下:

    GET方式

    通过 GET 调用时。params 类型为 Object,代表附带在 URL 中的参数(即 Query String)。
    HTTP 接口形式调用:
    curl -X GET "https://www.a.com/api/index?name=ckq"
    callContainer 形式调用:
    cloud.callContainer({ path:'/api/index?name=ckq', init:{ method:'GET', timeout: 60000,//ms }, success:({statusCode, header, data})=>{ console.log(data); }, fail: console.warn, complete: console.warn, })
    params 获得的内容
    { "params":{ "name": "ckq" } }

    获取 POST 参数

    当HTTP调用和 callContainer 使用 POST 方式调用云函数时,HTTP 请求中的 Request Body 会被转化为对应云函数中的 params 变量,开发者可在云函数中直接获取。
    根据请求 Content Type 的类型不同,params 也会有不同的类型。

    application/json

    application/json 为最常用的请求数据类型,此时 params 的类型为 Object,其值是对 Request Body 使用 JSON.parse 得到的。
    curl -X POST -d '{"name":"ckq"}' -H "Content-Type:application/json" \ https://www.a.com/api/index
    cloud.callContainer({ path:'/api/index', init:{ method:'POST', header:{ "content-type": "application/json", }, body:{ "name":"ckq", }, timeout: 60000,//ms }, success:({statusCode, header, data})=>{ console.log(data);}, fail: console.warn, complete: console.warn, })
    返回
    { "params": { "name": "ckq" } }

    application/x-www-form-urlencoded

    当请求的 Content Type 为 application/x-www-form-urlencoded 时,params 的类型也为 Object,其值是对 Request Body 使用 querystring.parse 解析获取得到的。
    curl -X POST -d 'name=ckq' \ -H "Content-Type:application/x-www-form-urlencoded" \ https://www.a.com
    cloud.callContainer({ path:'/api/index', init:{ method:'POST', header:{ "content-type": "application/x-www-form-urlencoded", }, body:"name=ckq" timeout: 60000,//ms }, success:({statusCode, header, data})=>{ console.log(data); }, fail: console.warn, complete: console.warn, })
    获取内容如下:
    { "params": { "name": "ckq" } }

    text/plain

    text/plain 一般用于发送纯文本内容,此时 params 的类型为 string,代表该文本的内容。
    curl -X POST -d "Some Text..." -H "Content-Type:text/plain" \ https://www.a.com
    cloud.callContainer({ path:'/api/index', init:{ method:'POST', header:{ "content-type": "text/plain", }, body:"Some Text...", timeout: 60000,//ms }, success:({statusCode, header, data})=>{ console.log(data);}, fail: console.warn, complete: console.warn, })
    获取内容如下:
    {"params": "Some Text..."}
    cloud.callContainer({ path:'/api/index', init:{ method:'POST', header:{ "content-type": "application/octet-stream", }, body:"Hello World", timeout: 60000,//ms }, success:({statusCode, header, data})=>{ console.log(data);}, fail: console.warn, complete: console.warn, })

    multipart/form-data

    multipart/form-data 一般用于上传文件,此时 params 的类型为 Object,并以 Key-Value 的形式直接对应请求数据。对于文件,如果key 值重复,后面的会覆盖前面的key对应的内容。其数据结构如下:
    type UploadFile = { name: string; // Name of the uploaded file type: string; // Mime type of the file size: number; // Size of the file in bytes buffer: Buffer; // Content of the entire file }
    发起请求如下:
    curl -X POST -F "abc=123" -F xyz=@hello.txt \ https://www.a.com
    小程序,小游戏侧建议使用cloud.uploadFile 上传文件至tos 存储。

    其他类型

    当使用其他类型(例如 application/octet-stream)时,params 的类型为 Buffer,代表请求的原始二进制数据。
    curl -X POST -d "Hello World" -H "Content-Type:application/octet-stream" \ https://www.a.com
    会得到如下结果:
    json
    { "params": { "type": "Buffer", "data": [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 ] } }

    控制面侧调试调用

    针对get 请求填写对应的 query 即可。
    针对post 请求在body 和 headers 中填写对应的内容和类型。暂不支持(multiple/form-data的调试)。

    context

    以HTTP方式调用云函数时候的请求上下文,可获取请求时的信息,例如请求头,请求方式等,也可设置响应头,响应状态码等。

    context.headers

    const contentType = context.headers['content-type']; const myCustomHeader = context.headers['x-my-header'];
    获取 HTTP 请求的 Headers,为键值对形式。
    对象中的键均为小写字母,例如应该是 context.headers['content-type'] 而非 context.headers['Content-Type']

    context.httpMethod

    获取 HTTP 请求的 Method,值为大写字母,例如 'POST''GET'

    context.path

    获取 HTTP 请求的完整 path。
    // /hello?hi=message; 获取得到的是 /hello const path = context.path;

    context.url

    获取 HTTP 请求的完整 URL,包括 path 和 query 字符串。
    // /hello?hi=message 获取得到的是/hello?hi=message const url = context.url;

    context.query

    获取 HTTP 请求的 Query String 转换的键值对象,在 GET 请求时与 params 相同。
    // ?name=ckq 获取值为ckq。 const name = context.query.name;

    context.cookies

    HTTP 请求request headers 中的cookie键值对。

    context.trigger

    获取云函数触发调用的来源,包含以下值:
    取值
    调用来源
    online
    线上callContainer和自定义域名调用
    ide_debug
    IDE调试
    console_debug
    控制面调试

    context.set(field, value)

    设置返回的 HTTP Response Headers 信息。
    context.set('content-type', 'application/json'); context.set('x-abc-header', 'hello world');

    context.remove(field)

    删除对应 field 的 HTTP Response Header 信息,若不存在该 field 则不会执行任何操作。

    context.setCookie(name,value, options)

    将名为 name 的 cookie 值设为 value
    参数
      {string} name:要设置的 cookie 的名称。
      {string} value:要设置的 cookie 值。
      {object} options:cookie 选项。
    具体的 options 选项参考 express response cookie

    context.clearCookie(name,options)

    将名为 name 的 cookie 值清空。
    context.clearCookie('token', { path: '/admin' });

    context.status(200)

    context.status(201);
    默认情况下函数执行成功返回的 Status Code 为 200,执行失败统一返回 500

    context.log

    context.log("hello");
    打印内容如下,默认打印时候带上logid 和函数名称。
    若您打印的是一个合法对象或者通过JSON.stringfy后的合法对象,则会在打印对象字段基础上添加logid 和 function 字段,因此需保证打印的对象内容中不包含logid和function字段,否则会被覆盖。
    打印内容如下:
    context.log({"name": "mike"});
    打印结果如下:
    ·
    可在日志平台界面过滤器模块中选择相关函数查看该函数的打印。
    context.debug、context.warn、context.error、context.info 与context.log 功能相同。