云函数Api
收藏
我的收藏

params​

概述​

params 为以http方式调用云函数时传递的参数,目前支持get post两种形式调用云函数,post 形式调用云函数时params内容格式根据请求头中的content-type 而定,详细描述如下:​

GET方式​

通过 GET 调用时。params 类型为 Object,代表附带在 URL 中的参数(即 Query String)。​
JavaScript
复制
curl -X GET \
"https://www.a.com/api/index?hello=world&abc=xyz&abc=123"
JavaScript
复制
cloud.callContainer({
path:'/api/index?hello=world&abc=xyz&abc=123',
init:{
method:'GET',
timeout: 60000,//ms
},
success:({statusCode, header, data})=>{
console.log(data);
},
fail: console.warn,
complete: console.warn,
})
返回​
JavaScript
复制
{
params:{
"hello": "world",
"abc": [ "xyz", "123" ]
}
}

获取 POST 参数​

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

application/json

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

application/x-www-form-urlencoded

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

text/

text/ 一般用于发送纯文本内容,此时 params 的类型为 string,代表该文本的内容。​
JSON
复制
curl -X POST -d "Some Text..." -H "Content-Type:text/plain" \
https://www.a.com
JavaScript
复制
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,
})
获取内容如下:​
JSON
复制
{
"params": "Some Text..."
}
JavaScript
复制
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对应的内容。其数据结构如下:​
JSON
复制
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
}
发起请求如下:​
JSON
复制
curl -X POST -F "abc=123" -F xyz=@hello.txt \
https://www.a.com
小程序,小游戏侧建议使用cloud.uploadFile 上传文件至tos 存储。​

其他类型​

当使用其他类型(例如 application/octet-stream)时,params 的类型为 Buffer,代表请求的原始二进制数据。​
JSON
复制
curl -X POST -d "Hello World" -H "Content-Type:application/octet-stream" \
https://www.a.com
会得到如下结果:​
json​
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​

Plain Text
复制
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​

Plain Text
复制
// /hello?hi=message
const path = context.path;
// => /hello
获取 HTTP 请求的完整 path。​

context.url​

Plain Text
复制
// /hello?hi=message
const url = context.url;
// => /hello?hi=message
获取 HTTP 请求的完整 URL,包括 path 和 query 字符串。​

context.query​

获取 HTTP 请求的 Query String 转换的键值对象,在 GET 请求时与 params 相同。​
JavaScript
复制
// ?hello=world&abc=xyz&abc=123
const hello = context.query.hello;
// => 'world'
const abc = context.query.abc;
// => [ 'xyz', '123' ]

context.cookies​

http 请求request headers 中的cookie键值对。​

context.trigger​

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

context.set(field, value)​

设置返回的 HTTP Response Headers 信息。​
JavaScript
复制
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 值清空。​
JavaScript
复制
context.clearCookie('token', { path: '/admin' });

context.status(200)​

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

context.log​

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