【发布时间】:2021-03-07 20:04:05
【问题描述】:
我正在节点 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
}
【问题讨论】:
标签: javascript node.js rest https get