# Help needed to code a HTTP GET inside a function

**URL:** <https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934>\
**Category:** General\
**Created:** [25 August 2022 22:16 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934 "2022-08-25T22:16:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![championc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/championc/32/39754_2.png) [@championc](https://discourse.nodered.org/u/championc)\
**Post date:** [25 August 2022 22:16 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/1 "2022-08-25T22:16:30Z")

</div>

Hi all,

I'm trying to run a HTTP GET and return a parsed JSON object in the function, to use some the data generated in and in the output of the function.

Let me explain - I have a water flow meter attached to our Rachio irrigation system. Right now I read the pulses and post to a graph every few seconds, with a generic topic of "garden". What I want to do is, when watering occurs, the first time the value exceeds zero, I need to fire the http request at the Rachio API to retrieve the Zone being watered. I then want to use the Zone from the API as the Topic being posted with the value to the graph. For every subsequent value over zero, I will use the Zone number already retrieved.

So I feel that I can handle everything pretty well within the Function - or is there a better way ?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 August 2022 07:46 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/2 "2022-08-26T07:46:59Z")

</div>

> [@championc](#):
>
> is there a better way

Certainly the node-red way would be to use an http node to perform the GET, not to do it inside the function.

---

<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:** [26 August 2022 07:58 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/3 "2022-08-26T07:58:17Z")

</div>

> [@championc](#):
>
> So I feel that I can handle everything pretty well within the Function - or is there a better way

Yes, since this is a low-code platform with built in http request node, then use the low code / ready built nodes.

> [@championc](#):
>
> What I want to do is, when watering occurs, the first time the value exceeds zero, I need to fire the http request at the Rachio API to retrieve the Zone being watered

Low code flow...  
 ![chrome_YtXzWf3Wwz](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a49ad757bd89ce85816f288678dd6d2f24c516e.gif)

Demo flow (use `CTRL+I) to import

```auto
[{"id":"4dd498cbf5b431aa","type":"inject","z":"b4c3cd7fb36660b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"measure_value","payload":"-5","payloadType":"num","x":1190,"y":120,"wires":[["c0a797ef8383c98c"]]},{"id":"c0a797ef8383c98c","type":"switch","z":"b4c3cd7fb36660b3","name":"> 0 ?","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1370,"y":160,"wires":[["cfafdd44217d9489"],["c171a0ed072099c1"]]},{"id":"9fb80e67a4652622","type":"inject","z":"b4c3cd7fb36660b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"measure_value","payload":"1","payloadType":"num","x":1200,"y":160,"wires":[["c0a797ef8383c98c"]]},{"id":"57a8fb62f0e49183","type":"inject","z":"b4c3cd7fb36660b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"measure_value","payload":"4","payloadType":"num","x":1200,"y":200,"wires":[["c0a797ef8383c98c"]]},{"id":"cfafdd44217d9489","type":"change","z":"b4c3cd7fb36660b3","name":"set true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":140,"wires":[["976a2b44444096d6"]]},{"id":"c171a0ed072099c1","type":"change","z":"b4c3cd7fb36660b3","name":"set false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":180,"wires":[["976a2b44444096d6"]]},{"id":"976a2b44444096d6","type":"rbe","z":"b4c3cd7fb36660b3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":1660,"y":160,"wires":[["90ac5696be6ced86"]]},{"id":"7d0f416fa10654c4","type":"debug","z":"b4c3cd7fb36660b3","name":"debug 71","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1660,"y":280,"wires":[]},{"id":"90ac5696be6ced86","type":"switch","z":"b4c3cd7fb36660b3","name":"true ?","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1330,"y":280,"wires":[["89f6852d6a17ec34"]]},{"id":"89f6852d6a17ec34","type":"http request","z":"b4c3cd7fb36660b3","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://jsonplaceholder.typicode.com/todos/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"Accept","keyValue":"","valueType":"application/json","valueValue":""}],"x":1490,"y":280,"wires":[["7d0f416fa10654c4"]]},{"id":"f312555c2a97776b","type":"comment","z":"b4c3cd7fb36660b3","name":"Only runs HTTP Req 1st time value is > 0","info":"","x":1260,"y":80,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![championc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/championc/32/39754_2.png) [@championc](https://discourse.nodered.org/u/championc)\
**Post date:** [26 August 2022 18:38 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/4 "2022-08-26T18:38:56Z")

</div>

Many thanks for that.

Since I will be using the http to just get a topic, I need to still retain the measure\_value. So should this work OK ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3a46661d7faac9268a0458f158ee4a04b1d7964.png)

---

<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:** [26 August 2022 18:59 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/5 "2022-08-26T18:59:12Z")

</div>

You could move the measurement payload to say msg.measurement and it should still be available after the http request node.

---

<div class="post-metadata">

**Author:** ![championc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/championc/32/39754_2.png) [@championc](https://discourse.nodered.org/u/championc)\
**Post date:** [26 August 2022 21:20 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/6 "2022-08-26T21:20:11Z")

</div>

In this way @E1cid ?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35d0223f4d80f8a70e2c9678d91353483bb45b34.png)

---

<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:** [26 August 2022 21:29 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/7 "2022-08-26T21:29:08Z")

</div>

The other way round.  
set msg.measure  
to msg.payload  
Then in your function node the measurement will be available in msg.measure.  
Presuming the measurement is in msg.payload.

---

<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:** [25 October 2022 21:29 UTC](https://discourse.nodered.org/t/help-needed-to-code-a-http-get-inside-a-function/66934/8 "2022-10-25T21:29:56Z")

</div>

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