• V
 

访问 HTTP 请求头

问题

你想访问请求中发送的 HTTP 头。

解决方案

使用来自 HTTP In 节点发送的消息的 msg.req.headers 属性来访问头信息。

示例

[{"id":"c1460268.3eba","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-headers","method":"get","swaggerDoc":"","x":130,"y":380,"wires":[["24199456.dbe66c"]]},{"id":"24199456.dbe66c","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        <h1>用户代理: {{req.headers.user-agent}}</h1>\n    </body>\n</html>","x":310,"y":380,"wires":[["b3531892.4cace8"]]},{"id":"b3531892.4cace8","type":"http response","z":"3045204d.cfbae","name":"","x":450,"y":380,"wires":[]}]
[~]$ curl http://localhost:1880/hello-headers
<html>
    <head></head>
    <body>
        <h1>用户代理: curl&#x2F;7.49.1</h1>
    </body>
</html>

讨论

msg.req.headers 属性是一个包含每个请求头的键/值对的对象。 无论请求中如何出现,头名称均为小写。