HTTP Request: set payload to append query string when method is set via msg.method

When using a HTTP Request node, there is the option of passing msg.method, but when setting this to GET I can't see the option of setting the payload option to Append to query-string parameters

Is this possible?

You can not see it when set to GET? It should look like this...
image

I need to do it in code.
I am passing in msg.method, so I also want to pass in that option.

Then you would need to construct the query string and attach it to the url
e.g.

[{"id":"c4b6402f.09b1f8","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":480,"y":100,"wires":[]},{"id":"3eea0560.938dfa","type":"function","z":"30af2d3e.d94ea2","name":"","func":"msg.method =\"GET\";\nmsg.url= \"http://some.site.com\";\nlet query_string = \"?\" + Object.entries(msg.payload).map(arr => arr[0] + \"=\" + encodeURIComponent(arr[1])).join(\"&\");\nmsg.url += query_string;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":120,"wires":[["c4b6402f.09b1f8","c44c2be6.fcde8"]]},{"id":"c44c2be6.fcde8","type":"http request","z":"30af2d3e.d94ea2","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":510,"y":140,"wires":[[]]},{"id":"8383d3e5.83f0a","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"one\":\"one_value?&/=.\",\"two\":\"two_value\"}","payloadType":"json","x":120,"y":100,"wires":[["3eea0560.938dfa"]]}]
msg.method ="GET";
msg.url= "http://192.168.1.25:1880/testing";
let query_string = "?" + Object.entries(msg.payload).map(arr => arr[0] + "=" + encodeURIComponent(arr[1])).join("&");
msg.url += query_string;
return msg;

making sure you encode any uri components.

I thought that - it's what I am doing at the moment.
Thanks

There is also a built in Node.js util called querystring
That could make the function code a tiny bit cleaner ..

but you have to require it in every function you want to use it.

image

Example Flow:

[{"id":"cbea7684686baa25","type":"inject","z":"54efb553244c241f","name":"obj params","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"foo\":\"bar\",\"baz\":[\"qux\",\"quux\"],\"corge\":\"test\"}","payloadType":"json","x":360,"y":3000,"wires":[["935992b107206cf1"]]},{"id":"935992b107206cf1","type":"function","z":"54efb553244c241f","name":"querystring","func":"msg.method =\"GET\";\nmsg.url= \"http://192.168.1.25:1880/testing\";\n\nmsg.url = msg.url + \"?\" + querystring.stringify(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"querystring","module":"querystring"}],"x":550,"y":3000,"wires":[["80283738e997d278"]]},{"id":"80283738e997d278","type":"debug","z":"54efb553244c241f","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":3000,"wires":[]}]
2 Likes

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