Payload + URL (msg.url) on Button press for POST request

Hi everyone. I have two variables entered by the user using text input. Let's call them variables A and B. Based on the value entered in Variable A, I have a function that sets a specific IP address as msg.url for HTTP request (POST). For instance, if the value A is 10, then the IP address defined in the function would be 192.168.0.10. In the payload of this request, structured as JSON are both variables A (10) and B (whatever other number the user enters).

I want the variables sent to the IP upon pressing a button. I have a change node that combines the variable in the payload and sets the msg.url. However, as you probably know, the button only sends the payload, so the . How can I send the IP of the request when the button is pressed? Any help will be very much appreciated.

Below the code.

[{"id":"dbbd9ac25d0245b7","type":"change","z":"af973086eb22abf7","name":"","rules":[{"t":"set","p":"temp","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.source","pt":"msg","to":"replySource","tot":"flow"},{"t":"set","p":"payload.code","pt":"msg","to":"replyCode","tot":"flow"},{"t":"set","p":"url","pt":"msg","to":"url","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":200,"wires":[["fac0982110da23a1"]]},{"id":"8a53079e89f84ec9","type":"debug","z":"af973086eb22abf7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1330,"y":140,"wires":[]},{"id":"b3864866e9cd631c","type":"ui_text_input","z":"af973086eb22abf7","name":"replySource","label":"Source","tooltip":"","group":"dfc9abd922154e9a","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":"300","topic":"","sendOnBlur":true,"className":"","topicType":"str","x":330,"y":160,"wires":[["ebdfa45c02f8ea4a"]]},{"id":"36d4bdfbad777b18","type":"ui_text_input","z":"af973086eb22abf7","name":"replyCode","label":"Code","tooltip":"","group":"dfc9abd922154e9a","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":"300","topic":"","sendOnBlur":true,"className":"","topicType":"str","x":330,"y":260,"wires":[["149b45117cbb284e"]]},{"id":"149b45117cbb284e","type":"change","z":"af973086eb22abf7","name":"","rules":[{"t":"set","p":"replyCode","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":260,"wires":[["dbbd9ac25d0245b7"]]},{"id":"ebdfa45c02f8ea4a","type":"change","z":"af973086eb22abf7","name":"","rules":[{"t":"set","p":"replySource","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":160,"wires":[["dbbd9ac25d0245b7","008fea195280df1e"]]},{"id":"eea42f6c187ac0fc","type":"http request","z":"af973086eb22abf7","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1330,"y":200,"wires":[[]]},{"id":"5fed2ff93e374d2a","type":"change","z":"af973086eb22abf7","name":"","rules":[{"t":"set","p":"url","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":120,"wires":[["dbbd9ac25d0245b7"]]},{"id":"008fea195280df1e","type":"function","z":"af973086eb22abf7","name":"","func":"var replySourceVal = flow.get('replySource');\n\nif (replySourceVal == \"10\") {\nmsg.payload = \"192.168.0.10/reply\";\n}\nif (replySourceVal == \"20\") {\nmsg.payload = \"192.168.0.20/reply\";\n}\nif (replySourceVal == \"30\") {\nmsg.payload = \"192.168.0.30/reply\";\n}\nif (replySourceVal == \"01\") {\nmsg.payload = \"192.168.0.101/reply\";\n}\nif (replySourceVal == \"02\") {\nmsg.payload = \"192.168.0.102/reply\";\n}\nif (replySourceVal == \"03\") {\nmsg.payload = \"192.168.0.103/reply\";\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":120,"wires":[["5fed2ff93e374d2a"]]},{"id":"be2bd6f7160e0d78","type":"debug","z":"af973086eb22abf7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":1320,"y":260,"wires":[]},{"id":"fac0982110da23a1","type":"ui_button","z":"af973086eb22abf7","name":"","group":"dfc9abd922154e9a","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"json","topic":"topic","topicType":"msg","x":1130,"y":200,"wires":[["8a53079e89f84ec9","eea42f6c187ac0fc","be2bd6f7160e0d78"]]},{"id":"dfc9abd922154e9a","type":"ui_group","name":"InputCommand","tab":"e562a4a.39a9e58","order":4,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e562a4a.39a9e58","type":"ui_tab","name":"Non-myth of the Noble Red's Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

In the send node for your button you can specify a flow variable which is sent as payload and contains the body of the POST request.

Dear @mickym2 thanks for the reply. I am still very new using node-red so I could not get your answer very well. I think the issue I am having is not about the payload itself, but about providing the msg.url data to the HTTP request once the button is clicked.

Ok then use the payload of the button only as a trigger and then use a change node which constructs the msg object completely new.
set msg.url and msg.payload from flow variable (you created before) and send the message into the http request node.

so instead of setting the msg.url in the change node before - do it after the button or just move the change node - as I saw behind your button node.

To press the button - is a complete new trigger and not depended on the construction of the info you want to send - but even if you pass the message through (then you do bypass the button) the msg object is constructed after the button node. So the payload which comes out of the button acts only as trigger and the content is irrelevant as the change node afterwards constructs the message.

In this case you have both the payload and the url in one msg object:

@mickym2 I really appreciate the time for explaining in such a detailed way. Thank you so much. I just tried what you suggested and worked like a charm. Danke!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.