• V
 

将 JSON 数据发布到流

问题

您希望将 JSON 数据发布到流中。

解决方案

使用 HTTP In 节点监听 Content-Type 设置为 application/json 的 POST 请求,并以 msg.payload 的属性形式访问解析后的 JSON。

示例

[{"id":"5b98a8ac.a46758","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-form","method":"post","swaggerDoc":"","x":120,"y":820,"wires":[["bba61009.4459f"]]},{"id":"bba61009.4459f","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        <h1>Hello {{ payload.name }}!</h1>\n    </body>\n</html>","x":290,"y":820,"wires":[["6ceb930a.93146c"]]},{"id":"6ceb930a.93146c","type":"http response","z":"3045204d.cfbae","name":"","x":430,"y":820,"wires":[]}]
[~]$ curl -X POST -d '{"name":"Nick"}' -H "Content-type: application/json" https://:1880/hello-form
<html>
    <head></head>
    <body>
        <h1>Hello Nick!</h1>
    </body>
</html>

讨论

HTTP In 节点收到 Content-Type 头设置为 application/json 的请求时,它会解析请求正文,并将数据通过 msg.payload 提供。

var name = msg.payload.name;