【问题标题】:How to get the request body off of a https get request using node js如何使用节点 js 从 https 获取请求中获取请求正文
【发布时间】: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


    【解决方案1】:
    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 || "");
    }
    

    【讨论】:

    • 对不起,我试图根据我的问题来破译这个,我应该把它放在哪里。我在请求函数中有我的 aws4.sign 包装器?你能否给我一个关于如何使用aws4做到这一点的提示,我有点迷茫。我已经看到了那个答案,但它对我没有帮助
    • 我将如何用我所拥有的实现 Promise 并执行 res.on('data') 部分我很抱歉,但我不太明白。我不知道从我的代码中添加或删除什么,所以我有点卡在这里
    • 我更新了我的答案,添加了我对代码外观的假设
    • 它似乎在工作,我会选择你的答案,但由于某种原因,当我尝试遍历对象时我得到未定义,我已经尝试过 JSON.parse(body...) any知道为什么会这样吗?
    • 我已经添加了我得到的 json 响应。这很奇怪,似乎可以正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2017-09-03
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多