在本地或者云端新建一个云函数后,统一有两个入参,params 和 context 如下,本文将介绍 params 和 context 的具体含义和如何使用。
/**
* @param params 调用参数,HTTP 请求下为请求体
* @param context 调用上下文
* @return 函数的返回数据,HTTP 场景下会作为 Response Body
*
*/
module.exports = async function (params, context) {
};
params 表示 以HTTP 域名方式或者 callContainer 调用云函数时传递的参数,目前支持get post两种形式调用,在调用云函数后云函数内能通过 params 获取具体调用入参,post 形式调用云函数时params内容格式根据请求头中的content-type 而定,详细描述如下:
通过 GET 调用时。
HTTP 接口形式调用:
curl -X GET "https://www.a.com/api/index?name=xyq"
callContainer 形式调用:
cloud.callContainer({
path:'/api/index?name=xyq',
init:{
method:'GET',
timeout: 60000,//ms
},
success:({statusCode, header, data})=>{
console.log(data);
},
fail: console.warn,
complete: console.warn,
})
params 获得的内容
{
"params":{
"name": "xyq"
}
}
当HTTP调用和 callContainer 使用 POST 方式调用云函数时,HTTP 请求中的 Request Body 会被转化为对应云函数中的
根据请求 Content Type 的类型不同,
curl -X POST -d '{"name":"xyq"}' -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":"xyq",
},
timeout: 60000,//ms
},
success:({statusCode, header, data})=>{
console.log(data);
},
fail: console.warn,
complete: console.warn,
})
返回
{
"params": { "name": "xyq" }
}
当请求的 Content Type 为
curl -X POST -d 'name=xyq' \ -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=xyq",
timeout: 60000,//ms
},
success:({statusCode, header, data})=>{
console.log(data);
},
fail: console.warn,
complete: console.warn,
})
获取内容如下:
{
"params": { "name": "xyq" }
}
text/plain 一般用于发送纯文本内容,此时
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..."}
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
当使用其他类型(例如
curl -X POST -d "Hello World" -H "Content-Type:application/octet-stream" \ https://www.a.com
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,
})
会得到如下结果:
{
"params": {
"type": "Buffer",
"data": [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 ]
}
}
以HTTP方式调用云函数时候的请求上下文,可获取请求时的信息,例如请求头,请求方式等,也可设置响应头,响应状态码等。
const contentType = context.headers['content-type']; const myCustomHeader = context.headers['x-my-header'];
获取 HTTP 请求的 Headers,为键值对形式。
对象中的键均为小写字母,例如应该是
获取 HTTP 请求的 Method,值为大写字母,例如
获取 HTTP 请求的完整 path。
// /hello?hi=message; 获取得到的是 /hello const path = context.path;
获取 HTTP 请求的完整 URL,包括 path 和 query 字符串。
// /hello?hi=message 获取得到的是/hello?hi=message const url = context.url;
获取 HTTP 请求的 Query String 转换的键值对象,在 GET 请求时与
// ?name=xyq 获取值为xyq。 const name = context.query.name;
HTTP 请求request headers 中的cookie键值对。
获取云函数触发调用的来源,包含以下值:
取值 | 调用来源 |
|---|---|
| online | 线上callContainer和自定义域名调用 |
| ide_debug | IDE调试 |
| console_debug | 控制面调试 |
设置返回的 HTTP Response Headers 信息。
context.set('content-type', 'application/json');
context.set('x-abc-header', 'hello world');
删除对应
将名为
参数
•
•
•
具体的
将名为
context.clearCookie('token', { path: '/admin' });
context.status(201);
默认情况下函数执行成功返回的 Status Code 为
context.log("hello");
打印内容如下,默认打印时候带上logid 和函数名称。
若您打印的是一个合法对象或者通过JSON.stringfy后的合法对象,则会在打印对象字段基础上添加logid 和 function 字段,因此需保证打印的对象内容中不包含logid和function字段,否则会被覆盖。
打印内容如下:
context.log({"name": "mike"});
打印结果如下:
可在日志平台界面过滤器模块中选择相关函数查看该函数的打印。
context.debug、context.warn、context.error、context.info 与context.log 功能相同。