Node-REDでAPI呼び出ししてみます
ここではぐるなびのAPIを使ってみます。Node-REDフローからこれらのAPIを呼び出し、その結果を確認します。
#1.ぐるなびのAPIを取得します
- ぐるなび Web Service
- ぐるなび レストラン検索API テストツール
- 画面中ほどの「URL」を確認(コピー)します
ぐるなびレストラン検索URL
https://api.gnavi.co.jp/RestSearchAPI/20150630/?keyid=9fc412c6c55fea80d6e91038edf75190&format=json
- 上記で入手されたURLを使ってこのあとのAPIアクセスを行います
#2.API呼び出しします
- 以下のフローをコピペします
API呼び出し
[{"id":"a2a1955a.409518","type":"inject","z":"13eb3eec.618729","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":60,"wires":[["7c5a8658.f4c918"]]},{"id":"7c5a8658.f4c918","type":"http request","z":"13eb3eec.618729","name":"","method":"GET","ret":"txt","url":"https://api.gnavi.co.jp/RestSearchAPI/20150630/?keyid=9fc412c6c55fea80d6e91038edf75190&format=json","tls":"","x":290,"y":60,"wires":[["9fafeee8.45e248"]]},{"id":"9fafeee8.45e248","type":"json","z":"13eb3eec.618729","name":"","pretty":false,"x":450,"y":60,"wires":[["fe6ec92.89a60b8","e7172d6.9db335"]]},{"id":"fe6ec92.89a60b8","type":"debug","z":"13eb3eec.618729","name":"","active":true,"console":"false","complete":"payload.rest.0.name","x":670,"y":60,"wires":[]},{"id":"e7172d6.9db335","type":"debug","z":"13eb3eec.618729","name":"","active":true,"console":"false","complete":"payload.rest.0.pr.pr_long","x":690,"y":100,"wires":[]}]
- 上記のフローの「http request」ノードをダブルクリックし、「URL」欄に前項で入手したURLをコピペします
#3.他のサービスからAPIとして呼び出されてみます
- 下記のフローは他のサービス(例としてDialogflow)からこのNode-REDフローがAPIとして呼び出される処理の例です
APIサービス化
[{"id":"fdcfb795.d1de28","type":"http in","z":"13eb3eec.618729","name":"","url":"/test30","method":"post","upload":false,"swaggerDoc":"","x":110,"y":60,"wires":[["2820269d.208fa2"]]},{"id":"24b1c21d.dcd4be","type":"http request","z":"13eb3eec.618729","name":"","method":"GET","ret":"txt","url":"","tls":"","x":530,"y":60,"wires":[["d612f1f5.e87b5"]]},{"id":"d612f1f5.e87b5","type":"json","z":"13eb3eec.618729","name":"","pretty":false,"x":690,"y":60,"wires":[["fa15393c.7705e","5008f65b.664318","85f120c5.a85808"]]},{"id":"5008f65b.664318","type":"debug","z":"13eb3eec.618729","name":"","active":false,"console":"false","complete":"payload.rest.0.pr.pr_long","x":910,"y":60,"wires":[]},{"id":"fa15393c.7705e","type":"debug","z":"13eb3eec.618729","name":"","active":true,"console":"false","complete":"payload.rest.0.name","x":890,"y":100,"wires":[]},{"id":"85f120c5.a85808","type":"function","z":"13eb3eec.618729","name":"restaurant","func":"msg.restaurant = msg.payload.rest[0].name;\nreturn msg;","outputs":1,"noerr":0,"x":840,"y":140,"wires":[["dd8a02bd.09d568","b5a20368.4a00b8"]]},{"id":"dd8a02bd.09d568","type":"debug","z":"13eb3eec.618729","name":"","active":false,"console":"false","complete":"restaurant","x":1040,"y":140,"wires":[]},{"id":"b5a20368.4a00b8","type":"function","z":"13eb3eec.618729","name":"speech: restaurant","func":"msg.payload = {\"speech\":msg.restaurant};\nreturn msg;","outputs":1,"noerr":0,"x":1030,"y":200,"wires":[["7b155c6f.13314c"]]},{"id":"7b155c6f.13314c","type":"http response","z":"13eb3eec.618729","name":"","statusCode":"","headers":{},"x":1090,"y":260,"wires":[]},{"id":"2820269d.208fa2","type":"function","z":"13eb3eec.618729","name":"パラメーター仕分け","func":"parm = \"https://api.gnavi.co.jp/RestSearchAPI/20150630/?\";\nparm += \"keyid=9fc412c6c55fea80d6e91038edf75190&format=json&\";\nparm += \"freeword=\"+msg.payload.result.parameters.customer_location;\nmsg.url = encodeURI(parm);\nreturn msg;\n","outputs":1,"noerr":0,"x":320,"y":60,"wires":[["24b1c21d.dcd4be"]]}]