【问题标题】:Get with NodeJS and Axios to a IoT broker使用 NodeJS 和 Axios 获取 IoT 代理
【发布时间】:2018-12-28 16:35:51
【问题描述】:

我想在已部署在虚拟机中的代理中创建 IoT 实体。目前我已经通过 curls 进行了交流,并且可以正常工作。

咨询“卷曲”:

(curl http://192.168.56.103:8080/ngsi10/updateContext -s -S --header 'Content-Type: application/json' \
--header 'Accept: application/json' -d @- | python -mjson.tool) <<EOF
{
    "contextElements": [{
        "entityId": {
            "id": "01",
            "type": "example"
        },
        "attributes": [{
            "name": "numbers",
            "type": "int",
            "contextValue": 24
        }],
        "domainMetadata": [{
            "name": "location",
            "type": "point",
            "value": {
                "latitude": 37.982636,
                "longitude": -1.123510
            }
        }]
    }],
    "updateAction": "UPDATE"
}
EOF

这对我来说工作正常。

这是nodejs使用axios的等效代码:

var config = {
    headers: {'Content-Type': 'application/json',
    'Accept': 'application/json'}
  };

  var dataJSon = [{
    "contextElements": [{
        "entityId": {
            "id": "01",
            "type": "example"
        },
        "attributes": [{
            "name": "numbers",
            "type": "int",
            "contextValue": 24
        }],
        "domainMetadata": [{
            "name": "location",
            "type": "point",
            "value": {
                "latitude": 37.982636,
                "longitude": -1.123510
            }
        }]
    }],
    "updateAction": "UPDATE"
}];

  const payload = {
    topic: 'topic',
    logs: dataJSon,
  };

  axios.get('http://192.168.56.103:8080/ngsi10/updateContext', dataJSon, config)
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

执行最后一个时,代理告诉我他不明白,我已经用wireshark 发出了请求,但他做得不好,数据没有出现,我只看到一个标题。 Los contenidos de los datos 儿子 JSON。

我会把它放在这里以防有人帮我。非常感谢您。

【问题讨论】:

    标签: json node.js curl axios


    【解决方案1】:

    -d 调用 发送一个POST 请求。使用axios.post

    axios.post('http://192.168.56.103:8080/ngsi10/updateContext', dataJSon, config)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2021-03-27
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2019-08-18
      相关资源
      最近更新 更多