我在节点js中使用https和aws4发出获取请求。我知道它有效是因为我的发布请求有效,但是对于我一生来说,我不知道如何获取GET请求正文。我需要将GET响应的请求正文放在一个变量中。
StatusCurrent函数是我需要获取GET请求发送回的响应的地方。
任何帮助将不胜感激。
const Alexa = require("ask-sdk-core");
var https = require("https");
var aws4 = require("aws4");
function request(opts) {
https
.request(opts, function (res) {
res.pipe(process.stdout);
// IS THIS WHERE I GET THE REQUEST BODY, BUT HOW
// I NEED TO PUT IT IN A VARIABLE
})
.end(opts.body || "");
}
function lightbulb(status) {
var load = JSON.stringify({
state: {
desired: {
on: status,
},
},
});
request(
aws4.sign(
{
hostname: "obsf-ats.iot.us-east-1.amazonaws.com",
service: "iotdata",
region: "us-east-1",
method: "POST",
path: "/things/esp8266_7F3B95/shadow",
headers: {
"Content-Type": "application/json0",
},
body: load,
},
{
secretAccessKey: "",
accessKeyId: "obsf",
}
)
);
}
function statusCurrent() {
request(
aws4.sign(
{
hostname: "obsf-ats.iot.us-east-1.amazonaws.com",
service: "iotdata",
region: "us-east-1",
method: "GET",
path: "/things/esp8266_7F3B95/shadow",
headers: {
"Content-Type": "application/json0",
},
},
{
secretAccessKey: "",
accessKeyId: "osbf",
}
)
);
}
根据答案,这是我返回的请求回复,但是我无法使用 body.state
{
"state": {
"desired": {
"on": false
},
"reported": {
"ota": {
"fw_version": "1.0",
"fw_id": "20201123-145524",
"mac": "4A3FDA7F3B95",
"device_id": "esp8266_7F3B95",
"app": "indoor-doorbell",
"arch": "esp8266",
"message": "idle",
"status": 0,
"is_committed": true,
"partition": 0,
"progress_percent": 0
},
"ram_total": 52672,
"ram_free": 28892,
"uptime": 0,
"btnCount": 2,
"on": false
}
},
"metadata": {
"desired": {
"on": {
"timestamp": 1606258639
}
},
"reported": {
"ota": {
"fw_version": {
"timestamp": 1606185982
},
"fw_id": {
"timestamp": 1606185982
},
"mac": {
"timestamp": 1606185982
},
"device_id": {
"timestamp": 1606185982
},
"app": {
"timestamp": 1606185982
},
"arch": {
"timestamp": 1606185982
},
"message": {
"timestamp": 1606218681
},
"status": {
"timestamp": 1606218681
},
"is_committed": {
"timestamp": 1606218681
},
"partition": {
"timestamp": 1606218681
},
"progress_percent": {
"timestamp": 1606218681
}
},
"ram_total": {
"timestamp": 1606218681
},
"ram_free": {
"timestamp": 1606171272
},
"uptime": {
"timestamp": 1606258639
},
"btnCount": {
"timestamp": 1606258639
},
"on": {
"timestamp": 1606258639
}
}
},
"version": 225030,
"timestamp": 1606265545
}
const httpGet = url => {
return new Promise((resolve, reject) => {
http.get(url, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => resolve(body));
}).on('error', reject);
});
};
参见https://stackoverflow.com/a/50244236/2142490
我希望它与https相同
更新:所以我会尝试
function request(opts) {
https
.request(opts, function (res) {
// IS THIS WHERE I GET THE REQUEST BODY, BUT HOW
// I NEED TO PUT IT IN A VARIABLE
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
console.log(body);
let json = JSON.parse(body);
console.log(json.state);
}
})
.end(opts.body || "");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句