次に示すような、HTTPエンドポイントに渡されたクエリパラメータにアクセスしたい。
http://example.com/hello-query?name=Nick
HTTP In
ノードに送信されたメッセージの msg.req.query
プロパティを使用して、
パラメータにアクセスします。
[{"id":"b34dd1af.4cb23","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 {{req.query.name}}!</h1>\n </body>\n</html>","x":290,"y":180,"wires":[["b828f6a6.47d708"]]},{"id":"1052941d.efad6c","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-query","method":"get","swaggerDoc":"","x":120,"y":180,"wires":[["b34dd1af.4cb23"]]},{"id":"b828f6a6.47d708","type":"http response","z":"3045204d.cfbae","name":"","x":430,"y":180,"wires":[]}]
[~]$ curl http://localhost:1880/hello-query?name=Nick
<html>
<head></head>
<body>
<h1>Hello Nick!</h1>
</body>
</html>
msg.req.query
プロパティは各クエリパラメータのキーと値のペアで成り立つオブジェクトです。
上記の例での /hello-query?name=Nick&colour=blue
へのリクエストの結果は次に示すものを含みます。
{
"name": "Nick",
"colour": "blue"
}
同じ名前の複数のクエリパラメータがある場合は、配列になります。
たとえば、/hello-query?colour=blue&colour=red
は次のとおりです。
{
"colour": ["blue","red"]
}
Node-RED: Low-code programming for event-driven applications.
Copyright OpenJS Foundation and Node-RED contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
The OpenJS Foundation | Terms of Use | Privacy Policy | OpenJS Foundation Bylaws | Trademark Policy | Trademark List | Cookie Policy