Asynchronous Request-Reply

DESCRIPTION: This subflow is used to send requests to certain URLs. There are two request types which are used in this flow. The POST and the GET requests. The responses come in msg.payload as a JSON string which then turned into an object.

request_reply

DETAILS:

  • The user can set the URL and the request type on the properties page. They can also assign msg.requestType or msg.url variables into their message object.

  • If the user tries to send the input variables inside message object and also set them in the properties page, the used variables will be the ones on the properties page.

IMPORTANT NOTE: Make sure to read the request expectancies and response types of websites. They may vary a lot.

[{"id":"f47d8cbf9d50c5b1","type":"subflow","name":"Subflow 13","info":"","category":"","in":[{"x":60,"y":240,"wires":[{"id":"ee38901f53632cbf"}]}],"out":[{"x":1260,"y":240,"wires":[{"id":"f580ceffb3d1282e","port":0},{"id":"866ba505d970d8eb","port":0}]}],"env":[{"name":"requestType","type":"str","value":"","ui":{"icon":"font-awesome/fa-search-plus","type":"select","opts":{"opts":[{"l":{"en-US":"OnMessage"},"v":""},{"l":{"en-US":"POST"},"v":"POST"},{"l":{"en-US":"GET"},"v":"GET"}]}}},{"name":"URL","type":"str","value":""}],"meta":{},"color":"#DDAA99"},{"id":"f580ceffb3d1282e","type":"http request","z":"f47d8cbf9d50c5b1","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":910,"y":120,"wires":[[]]},{"id":"866ba505d970d8eb","type":"http request","z":"f47d8cbf9d50c5b1","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":910,"y":360,"wires":[[]]},{"id":"4be7495163fda84e","type":"function","z":"f47d8cbf9d50c5b1","name":"","func":"let msg1;\nlet msg2;\n\nif(msg.requestType1){\n    msg.requestType = msg.requestType1;\n}\n\n\nif(msg.requestType === \"GET\"){\n    msg1 = msg;\n} else if(msg.requestType === \"POST\") {\n    msg2 = msg;\n}\n\nreturn [msg1, msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":240,"wires":[["db0448ccc8d5d98d"],["f2a80531f8a601b7"]]},{"id":"ee38901f53632cbf","type":"change","z":"f47d8cbf9d50c5b1","name":"","rules":[{"t":"set","p":"requestType1","pt":"msg","to":"requestType","tot":"env"},{"t":"set","p":"firstUrl","pt":"msg","to":"URL","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":240,"wires":[["4be7495163fda84e"]]},{"id":"f2a80531f8a601b7","type":"function","z":"f47d8cbf9d50c5b1","name":"","func":"\nif(msg.firstUrl){\n    msg.url = msg.firstUrl;\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":360,"wires":[["866ba505d970d8eb"]]},{"id":"db0448ccc8d5d98d","type":"function","z":"f47d8cbf9d50c5b1","name":"","func":"\nif(msg.firstUrl){\n    msg.url = msg.firstUrl;\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":120,"wires":[["f580ceffb3d1282e"]]}]

Collection Info

Flow Info

Created 2 years, 2 months ago
Rating:

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • function (x3)
  • http request (x2)
Other
  • subflow (x1)

Tags

  • Request-Reply
  • Cloud
  • Backend
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option