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

**URL:** <https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675>\
**Category:** General\
**Created:** [27 July 2022 08:42 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675 "2022-07-27T08:42:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MyRandomThoughts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myrandomthoughts/32/41085_2.png) [@MyRandomThoughts](https://discourse.nodered.org/u/MyRandomThoughts)\
**Post date:** [27 July 2022 08:42 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/1 "2022-07-27T08:42:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2022 08:53 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/2 "2022-07-27T08:53:54Z")

</div>

> [@MyRandomThoughts](#):
>
> but when setting this to `GET` I can't see the option of setting the payload option to `Append to query-string parameters`

You can **not** see it when set to `GET`? It **should** look like this...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11b128ad47a253069abbf12d20f904e6da0b0528.png)

---

<div class="post-metadata">

**Author:** ![MyRandomThoughts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myrandomthoughts/32/41085_2.png) [@MyRandomThoughts](https://discourse.nodered.org/u/MyRandomThoughts)\
**Post date:** [27 July 2022 09:26 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/3 "2022-07-27T09:26:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 July 2022 09:57 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/4 "2022-07-27T09:57:00Z")

</div>

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

```auto
[{"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"]]}]

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![MyRandomThoughts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/myrandomthoughts/32/41085_2.png) [@MyRandomThoughts](https://discourse.nodered.org/u/MyRandomThoughts)\
**Post date:** [27 July 2022 10:35 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/5 "2022-07-27T10:35:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 July 2022 15:40 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/6 "2022-07-27T15:40:29Z")

</div>

There is also a built in Node.js util called [querystring](https://nodejs.org/api/querystring.html#querystringstringifyobj-sep-eq-options)  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/7716a6d8b3fb47cff9d1adfdd148a4a807b9dbbd.png)

**Example Flow:**

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 August 2022 15:40 UTC](https://discourse.nodered.org/t/http-request-set-payload-to-append-query-string-when-method-is-set-via-msg-method/65675/7 "2022-08-10T15:40:36Z")

</div>

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