无论使用post请求或者get请求请求传递的数据,永远是空,也尝试通过queryStringParameters和body进行解包,但是return event就是空数据,所以不行,云函数目前直接return event,使用apipost发送数据,求大佬指点迷津。
- 发布:2025-01-08 00:34
- 更新:2025-01-08 10:39
- 阅读:51
套马杆的套子 - 没有解决不了的问题,只有解决不完的问题
你看下,云对象我这么用的,
请求url是https://*****/web/test?title=todo-title&content=todo-content的,
module.exports = {
_before: function() { // 通用预处理器
const httpInfo = this.getHttpInfo()
console.log('33333333333', httpInfo);
},
test(param1) {
console.log('111111111111', param1);
return param1
}
}
test里的1111111会打印{
"title": "todo-title",
"content": "todo-content"
},
如果是放到body json里,参数是{
"param1":"2222"
}
打印就是
33333333333 {
path: '/test',
httpMethod: 'POST',
headers: {
accept: '*/*',
'accept-encoding': 'gzip, deflate',
'cache-control': 'no-cache',
'content-length': '19',
'content-type': 'application/json',
'postman-token': 'cdd1243c-9c81-467b-92c0-5c9b44cdde7a',
service: 'web-fun',
'sofa-rpcid': '0a4b07e317362984980435906233',
'sofa-traceid': '0a4b07e317362984980435906233',
sofa_head_method_name: '/web/test',
'user-agent': 'PostmanRuntime/7.4.0',
'x-djy-uuid': 'f0e914a7-c83c-4495-be07-52589ad30d7d',
'x-faas-context-bindconfigs': '{"NEED_AUTH":"false","NEED_SIGN":"false"}',
'x-faas-context-binddomain': 'env-00jxhoob9v1j.dev-hz.cloudbasefunction.cn',
'x-faas-context-bindpath': '/web',
'x-faas-runtime-call-context': '{"appId":"2021004147642798","traceId":"0a4b07e317362984980435906233","rpcId":"0.1.2","requestId":"891b471c-1cba-48e8-88f6-b655b3debbba","eventType":"http"}',
'x-faas-source-type': 'httpDefault',
'x-forwarded-for': '124.237.76.85',
'x-forwarded-proto': 'https',
'x-real-ip': '124.237.76.85',
'x-stream-request-id': 'e1c327a0-e04a-491f-a18e-b4a7626d2169',
'x5-uuid': 'f0e914a7-c83c-4495-be07-52589ad30d7d'
},
queryStringParameters: { title: 'todo-title', content: 'todo-content' },
body: '{"param1":"2222"}',
isBase64Encoded: false
}
-
1***@qq.com (作者)
回复 套马杆的套子: 我测试的阿里云和支付宝云2个云空间,阿里云空间正常return返回数据,但是支付宝云只有云函数运行日志里面是有内容的,但是return不出来,event无法引用内容,你用的是阿里的云空间吗
2025-01-08 16:39
1***@qq.com (作者)
官方示例:
使用GET请求https://${云对象Url化域名}/${触发路径}/${云对象方法名}?a=1&b=2,云对象接收到的event为
{
path: '/${云对象方法名}',
httpMethod: 'GET',
headers: {HTTP请求头},
queryStringParameters: {a: "1", b: "2"},
isBase64Encoded: false
}
云函数日志:
2025-01-08 10:32:57.092 [adminAnnouncement][USER][INFO][0a4b07bb17363035770824041221] execute: d688a8f6-76a2-4103-9929-273cbe3fcabe,{
path: '/addAnnouncement',
httpMethod: 'GET',
headers: {
accept: '/',
'accept-encoding': 'gzip, deflate, br',
'cache-control': 'no-cache',
'content-length': '454',
'content-type': 'multipart/form-data; boundary=--------------------------964494404528125580825484',
service: 'adminAnnouncement',
'sofa-rpcid': '0a4b07bb17363035770824041221',
'sofa-traceid': '0a4b07bb17363035770824041221',
sofa_head_method_name: '/announcement/addAnnouncement',
'user-agent': 'PostmanRuntime-ApipostRuntime/1.1.0',
'x-djy-uuid': '6a81c371-1b09-4b27-8251-c5af3d20f368',
'x-faas-context-bindconfigs': '{"NEED_AUTH":"false","NEED_SIGN":"false"}',
'x-faas-context-binddomain': 'env-00jxhew1a2ph.dev-hz.cloudbasefunction.cn',
'x-faas-context-bindpath': '/announcement',
'x-faas-runtime-call-context': '{"appId":"2021004182661748","traceId":"0a4b07bb17363035770824041221","rpcId":"0.1.2","requestId":"233e72b4-fb9d-48db-848b-98b9e286d803","eventType":"http"}',
'x-faas-source-type': 'httpDefault',
'x-forwarded-for': '39.154.168.205',
'x-forwarded-proto': 'https',
'x-real-ip': '39.154.168.200',
'x-stream-request-id': 'ec1cf0cb-85a9-4fa5-a950-ebf55dabe1cf',
'x5-uuid': '6a81c371-1b09-4b27-8251-c5af3d20f368'
},
queryStringParameters: {},
body: '',
isBase64Encoded: ''
}
apipost:发送body下form-data数据,但是云函数内还是接收不到,使用url?字段可以接收,是我apipost使用的不对吗?(图片可以参考正文)
1***@qq.com (作者)
我尝试过这个,一样也是空
2025-01-08 08:49
套马杆的套子
回复 1***@qq.com: 我刚试了下都是可以的,,post请求,第一种方式,url后+?title=todo-title&content=todo-content
第二种方式,我上面发的那种,在_before: function() { // 通用预处理器
const httpInfo = this.getHttpInfo()
console.log('33333333333', httpInfo);
},
都可以拿到
2025-01-08 09:09
1***@qq.com (作者)
回复 套马杆的套子: 谢谢大佬解答,添加 通用预处理器的方式发送post请求确实可以,我忽略预处理器了,发生get请求的话,queryStringParameters还是空,需要在预处理添加什么内容才可以显示出来呢,谢谢回复
2025-01-08 10:22
1***@qq.com (作者)
回复 1***@qq.com: 解决了,还是apipost使用的问题,遵循官方文档即可
2025-01-08 11:08
1***@qq.com (作者)
回复 1***@qq.com: 谢谢大佬解答
2025-01-08 11:08