【问题标题】:HTTP/JSON request formatting from Node-Red to Google Cloud Functions从 Node-Red 到 Google Cloud Functions 的 HTTP/JSON 请求格式
【发布时间】:2019-02-11 17:13:42
【问题描述】:

我很难确定我从 node-red 到 Google 云的 HTTP 请求的格式是否正确。我在谷歌云功能中设置了一个非常简单的功能,只是为了检查什么是入站的,以及是否一切都被正确传递。我向它传递了几个 JSON 数据字段并返回一个随机数据字段以进行测试。

exports.helloWorld = function helloWorld(req, res)
{
  var myResponse = req.body.topic;
  if(req.get('content-type') === "application/json")
  {
    myResponse += " - Json";
  }


  res.status(200).send(myResponse);
};

我在这里以两种方式读取 JSON 数据。服务器响应字符串:“undefined - Json”,这意味着它没有正确读取 req.body.topic。据我了解,gcloud 将根据 content-type 标头解析 http 请求正文中的数据。这个参数显然是正确读取的,但其余的不是。

在红色节点中,我将以下数据传递给 HTTP 请求节点。我将我的数据字段作为对象附加到 HTTP 请求中的正文字段。

{
   "url":"http://localhost:8010/my-smart-home-xxxx/us-central1/helloWorld",
   "body":
      {
         "topic":"smartGarden/001/moistureLevel",
         "payload":"11",
         "origin":"node-red-local"
       },
    "headers":
      {
        "content-type":"application/json"
      },
        "_msgid":"2aa44dc.b7391b2"
}

我对此有点陌生,所以我可能犯了一个非常基本的错误。

感谢您的帮助!

(我目前正在运行 gcloud 函数模拟器,但在实际云环境中得到了相同的结果。)

【问题讨论】:

    标签: json google-cloud-platform httprequest node-red


    【解决方案1】:

    接收输入并验证content-type 是否为json 的正常做法是运行JSON.parse() 之类的东西,将其从字符串转换为json。

    【讨论】:

    • 感谢您的反馈。然而这不起作用?无论我是否使用 JSON.parse() 函数,我都会遇到同样的问题。
    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2020-07-05
    • 2022-01-15
    相关资源
    最近更新 更多