Reputation: 1545
I am making a get request using https and aws4 in node js. I know it works because my post request works, but for the life of me I can't figure out how to get the GET request body. I need to put the request body of the GET response in a variable.
The StatusCurrent function is where i need to get the response sent back by the GET Request.
Any help would be greatly appreciated.
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",
}
)
);
}
based on the answer this is the request repsonse I get back but I can't access the state with 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
}
Upvotes: 0
Views: 2214
Reputation: 783
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);
});
};
see https://stackoverflow.com/a/50244236/2142490
I'd expect it to be the same for https
UPDATE: so i'd try
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 || "");
}
Upvotes: 3