Node-REDでhttp requestノードでAPIを実行する

Node-REDでhttp requestノードでAPIを実行する

Node-REDで作成したREST APIをwindowsのcurlコマンドで叩くことができますが、こちらのcurlもhttp requestノードを使用すれば代用することができます。

Node-REDでhttp requestノードでAPIを実行する

簡単なPOSTメソッドのREST APIを作成します。

項目
HTTPメソッド POST
path /hoge
Node-RED localhost:1880

Node-REDでhttp requestノードでAPIを実行する

http responseは配置して200を返すだけです。msg.statusCodeにこの値が入ります。

Node-REDでhttp requestノードでAPIを実行する

デバッグしているのは以下2つです。

  • msg.req.body
  • msg.headers.x-auth-key

msg.req.headers プロパティは、各リクエストパラメータのキーと値のペアで構成されたオブジェクトです。 ヘッダ名は、リクエストの内容にかかわらず、すべて小文字です。

この状態でデプロイして、curlで叩くことができます。

sample.json

{
  "name": "takahashi",
  "age": 20
}

curl

C:\Users\takahashi-h5>curl -X POST -d @sample.json -H "Content-type: application/json" http://localhost:1880/hoge
{"name":"takahashi","age":20}

今回はこのcurl部分をhttp requestノードでやってみたいと思います。

http requestノードを使う

同一フローにinjectionノードから始まるフロー作成します。

Node-REDでhttp requestノードでAPIを実行する

functionノードでリクエストヘッダの設定、msg.payloadにJSONを設定しています。msg.payloadの値がREST APIのリクエストボディ(msg.req.body)に渡されるようです。

Node-REDでhttp requestノードでAPIを実行する

http requestノードでは「http://localhost:1880/hoge」をPOSTで呼び出しています。

出力形式は「JSONオブジェクト」に変更します。

Node-REDでhttp requestノードでAPIを実行する

デプロイして実行します。

Node-REDでhttp requestノードでAPIを実行する

フロー

[{"id":"ec6d3a8ac62ced5f","type":"tab","label":"フロー 1","disabled":false,"info":"","env":[]},{"id":"5b98a8ac.a46758","type":"http in","z":"ec6d3a8ac62ced5f","name":"","url":"/hoge","method":"post","upload":false,"swaggerDoc":"","x":110,"y":60,"wires":[["8eabd308.4c855","fe4e531b.95f61","39c767eea91bf4f4"]]},{"id":"e16244b9.c94a58","type":"function","z":"ec6d3a8ac62ced5f","name":"","func":"// header set\nmsg.headers = {};\nmsg.headers['content-type'] = 'application/json';\nmsg.headers['X-Auth-Key'] = 'fred-key';\n// payloadにリクエストボディ部をset\nmsg.payload = {\"name\":\"takahashi\",\"age\":20};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":240,"wires":[["8a2c5cb2f126b394"]]},{"id":"adb52c94.6978a","type":"inject","z":"ec6d3a8ac62ced5f","name":"curl command","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":110,"y":240,"wires":[["e16244b9.c94a58"]]},{"id":"8eabd308.4c855","type":"debug","z":"ec6d3a8ac62ced5f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"req.body","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":60,"wires":[]},{"id":"47f301d3.2508","type":"debug","z":"ec6d3a8ac62ced5f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"statusCode","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":240,"wires":[]},{"id":"fe4e531b.95f61","type":"http response","z":"ec6d3a8ac62ced5f","name":"","statusCode":"200","headers":{},"x":380,"y":160,"wires":[]},{"id":"39c767eea91bf4f4","type":"debug","z":"ec6d3a8ac62ced5f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"req.headers.x-auth-key","targetType":"msg","statusVal":"","statusType":"auto","x":440,"y":100,"wires":[]},{"id":"8a2c5cb2f126b394","type":"http request","z":"ec6d3a8ac62ced5f","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://localhost:1880/hoge","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":450,"y":240,"wires":[["47f301d3.2508"]]}]

参考サイト

https://cookbook.nodered.jp/http/set-request-header
https://cookbook.nodered.jp/http/access-http-request-headers
https://cookbook.nodered.jp/http/parse-json-response

コメント

タイトルとURLをコピーしました