• V
 

将表单数据发送到流程

问题

您想要将表单数据发送到一个流程中。

解决方案

使用 HTTP In 节点监听 Content-Type 设置为 application/x-www-form-urlencoded 的 POST 请求,并将表单数据作为 msg.payload 的属性访问。

示例

[{"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" http://localhost:1880/hello-form
<html>
    <head></head>
    <body>
        <h1>Hello Nick!</h1>
    </body>
</html>

讨论

HTML 表单可以用来将数据从浏览器发送回服务器。如果配置为 POST 数据,浏览器会使用 content-typeapplication/x-www-form-urlencoded 的方式对 <form> 中持有的数据进行编码。

例如,当提交一个看起来像这样的表单时:

<form action="http://localhost:1880/hello-form" method="post">
  <input name="name" value="Nick">
  <button>说你好</button>
</form>

它会产生如下请求:

POST / HTTP/1.1
Host: localhost:1880
Content-Type: application/x-www-form-urlencoded
Content-Length: 9

name=Nick

HTTP In 节点收到这样的请求时,它会解析请求的主体,并将表单数据提供给 msg.payload

var name = msg.payload.name;