大家好!我在通过小程序访问外网数据时,遇到了问题,访问的网址是移动OneNet平台,我已经设置了request域名为 https://api.heclouds.com,情况如下:
1、在小程序端,直接使用wx.request访问,没有问题,代码如下:
wx.request({
url: "https://api.heclouds.com/devices/"+ID+"/datapoints",
header: {
"api-key": "*********="
},
success (res) {
console.log("Net:",res.data);
}
})
2、当我将代码改为通过云函数访问时,采用的是request-promise模块,始终报错:
{"errno":6,"error":"invalid parameter: auth by api-key or authorization"}
代码如下:
var rp=require("request-promise");
var option={
url: "https://api.heclouds.com/devices/12345678/datapoints",
method: "GET",
json: true,
qs: {
"api-key": "******="
},
header: {
"content-type": "application/json",
"User-Agent": "Request-Promise"
}
};
await rp(option)
.then(function (repos) {
console.log('User has repos', repos.length);
})
3、我测试了request和requset-promise两种访问方式,都一样不行。
4、我测试了安装wx-server-sdk,再安装request和request-promise,不行。
5、我测试了只安装wx-server-sdk,并使用它自带的request版本,也不行。
6、移动的api-key 字符串里面有一个等号字符,传输时变成%3D,是不是这个字符的问题?但在小程序端又是可以的。
还望大家不吝赐教,谢谢!
header: { "content-type": "application/json",
"User-Agent": "Request-Promise"
}
应该写为headers , 多一个S 就可以了
一开始写在header里的,不行,才换到qs里的。
你在wx.request里写在header里,为什么到了request-promise里就换成qs里呢?