# Save value from a request

**URL:** <https://discourse.nodered.org/t/save-value-from-a-request/33359>\
**Category:** General\
**Created:** [25 September 2020 21:57 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359 "2020-09-25T21:57:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [25 September 2020 21:57 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/1 "2020-09-25T21:57:39Z")

</div>

Hi,

I want to save msg.keyID from a request, which returns more than 1 times. I think a picture maybe good to demonstrate.  
One time request return these value..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19a4c01023ca784ab14062c86aa6e1f34d23f9b5.png)

and here is my currently code

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

```auto
[{"id":"e2a6bf49.da7258","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ad06247.c845bd8","type":"ui_button","z":"e2a6bf49.da7258","name":"GET Keys","group":"b8575647.ebefb","order":6,"width":0,"height":0,"passthru":false,"label":"GET DEFINED OBJECTS","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"num","topic":"","x":70,"y":160,"wires":[["fc127236.89ff4"]]},{"id":"fc127236.89ff4","type":"function","z":"e2a6bf49.da7258","name":"GET Keys","func":"return {payload : \"KEYS *\"};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":230,"y":160,"wires":[["b260546d.7c5e1"]]},{"id":"b260546d.7c5e1","type":"http request","z":"e2a6bf49.da7258","name":"Get Keys","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":380,"y":160,"wires":[["eccbf81f.7a8508"]]},{"id":"eccbf81f.7a8508","type":"function","z":"e2a6bf49.da7258","name":"Response","func":"var definedKeys = msg.payload.keys;\nreturn {payload : definedKeys};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":160,"wires":[["31658d6b.039eca"]]},{"id":"ee088fb8.f544b","type":"http request","z":"e2a6bf49.da7258","name":"HTTP GET REQUEST","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":460,"y":300,"wires":[["3741ec9.6c07394"]]},{"id":"3741ec9.6c07394","type":"debug","z":"e2a6bf49.da7258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":240,"wires":[]},{"id":"31658d6b.039eca","type":"split","z":"e2a6bf49.da7258","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":110,"y":300,"wires":[["25cfb7bd.8a95b8"]]},{"id":"25cfb7bd.8a95b8","type":"function","z":"e2a6bf49.da7258","name":"","func":"msg.keyID = msg.payload;\nmsg.payload = 'SCAN%20'+msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":300,"wires":[["ee088fb8.f544b"]]},{"id":"dac2dc01.50f688","type":"ui_text","z":"e2a6bf49.da7258","group":"b8575647.ebefb","order":1,"width":0,"height":0,"name":"","label":"KeyID","format":"{{msg.keyID}}","layout":"row-spread","x":640,"y":740,"wires":[]},{"id":"18cb72ac.e7224d","type":"ui_text","z":"e2a6bf49.da7258","group":"b8575647.ebefb","order":2,"width":0,"height":0,"name":"","label":"Position","format":"{{msg.payload}}","layout":"row-spread","x":650,"y":820,"wires":[]},{"id":"bc020133.4241d","type":"json","z":"e2a6bf49.da7258","name":"","property":"payload","action":"","pretty":false,"x":310,"y":680,"wires":[[]]},{"id":"b8575647.ebefb","type":"ui_group","z":"","name":"Default","tab":"d9869613.8ce11","order":1,"disp":true,"width":"6","collapse":false},{"id":"d9869613.8ce11","type":"ui_tab","z":"","name":"Case 10: Get all defined objects","icon":"dashboard","order":10,"disabled":false,"hidden":false}]

```

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 September 2020 07:23 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/2 "2020-09-26T07:23:12Z")

</div>

Where is it that you want to use the id?

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [26 September 2020 08:03 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/3 "2020-09-26T08:03:02Z")

</div>

Situation is:

- I am using platform "Tile38", which uses to define and determine geology subject.
- I want to get all defined objects in Tile38. Every object has a keyID, a filed name, longitutde and latitude (position of object). It could be, more than one object has the same keyID but different filed name.

My plan is:

- Firstly, get all defined keyID in database with a URL request "KEY \*".
- Secondly, using these keyID to get field name and position of objects with another URL request "SCAN\*".

I have wrote a small Java program with this strategy and it works fine. I have no experience with Javascript but I think it should work same.

Sorry for my bad English.

---

<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 September 2020 08:12 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/4 "2020-09-26T08:12:20Z")

</div>

Let me ask the question another way, what doesn't work in the flow you posted?

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [26 September 2020 08:23 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/5 "2020-09-26T08:23:37Z")

</div>

It works but like you see it returns 3 times for 3 keyID in just one time request. It's maybe bigger if I define more object in platform. I want to save 3 keyID, field name, position in a map or dictionary, whatever data structure which have key, value with key = keyID + field name, value = position object and then display these objects in a text output.

---

<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 September 2020 08:34 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/6 "2020-09-26T08:34:48Z")

</div>

> [@torrent](#):
>
> it returns 3 times for 3 keyID in just one time request.

Sorry, I don't understand what you mean, do you mean that bit is working correctly but now you want to save those so you can use them later? If that is what you mean do you actually need to save them or do you just want to display them?

If you want to save the data then have a look at the node red docs page Working with Context which will show you how you can save the data in an object and then use it later.

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [26 September 2020 08:41 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/7 "2020-09-26T08:41:13Z")

</div>

Right, I want to save them to use later and also display them. And the text element in nodered dashboard doesn't work for this situation, do you have any suggestion?

---

<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 September 2020 08:53 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/8 "2020-09-26T08:53:58Z")

</div>

What do you want the display to look like? Sketch what you would like to see from the debug that you posted earlier.

---

<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 September 2020 09:16 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/9 "2020-09-26T09:16:51Z")

</div>

@torrent

It is not 100% clear what you want so I did a little bit of guess work and generated a flow that does this...

1. add an object to `flow` context called `results`
2. upon each HTTP GET REQ, add the `payload` into the `flow.results` object under the key 'keyID`

```auto
[{"id":"5518f8ee.37a1f8","type":"http request","z":"f057b3e5.0b042","name":"Get Keys","method":"GET","ret":"obj","paytoqs":true,"url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":520,"y":80,"wires":[["f5dfb751.43a2c8"]]},{"id":"2fe3f84b.4cd0b8","type":"http request","z":"f057b3e5.0b042","name":"HTTP GET REQUEST","method":"GET","ret":"obj","paytoqs":true,"url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":580,"y":160,"wires":[["50e1504e.9179a"]]},{"id":"15ede35b.a9535d","type":"split","z":"f057b3e5.0b042","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":250,"y":160,"wires":[["ecc212dd.0fec1"]]},{"id":"ecc212dd.0fec1","type":"function","z":"f057b3e5.0b042","name":"","func":"msg.keyID = msg.payload;\nmsg.query = 'SCAN%20' + msg.payload;\nmsg.payload = msg.query;\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":160,"wires":[["2fe3f84b.4cd0b8"]]},{"id":"cf93726d.aa6b1","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["1c96af81.c5fe2"]]},{"id":"1c96af81.c5fe2","type":"change","z":"f057b3e5.0b042","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"KEYS *","tot":"str"},{"t":"set","p":"results","pt":"flow","to":"{}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":80,"wires":[["5518f8ee.37a1f8"]]},{"id":"f5dfb751.43a2c8","type":"change","z":"f057b3e5.0b042","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.keys","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":80,"wires":[["15ede35b.a9535d"]]},{"id":"50e1504e.9179a","type":"function","z":"f057b3e5.0b042","name":"Store in flow.results","func":"//store payload from HTTP GET REQ\n//in the results object, using keyID as the key\n\nvar results = flow.get(\"results\") || {};//get the results object\nresults[msg.keyID] = msg.payload; //add this result\nflow.set(\"results\", results); //update flow storage\n\nreturn msg;","outputs":1,"noerr":0,"x":820,"y":160,"wires":[[]]}]

```

Give it a go, then check the context tab on the side-bar - see if it works for you.

NOTE: I cannot test this as I dont have access to your URL localhost:9851

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [26 September 2020 09:46 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/10 "2020-09-26T09:46:24Z")

</div>

The output should look like:  
"Object with keyID "+ keyID + " and field name "+ fieldName +" longitude: "+longitude+" latitude "+latitude  
and it displays with this form for all objects

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [26 September 2020 09:47 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/11 "2020-09-26T09:47:03Z")

</div>

Hi, you could try this platform from Docker.

```auto
docker pull tile38/tile38
docker run -p 9851:9851 tile38/tile38

```

---

<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 September 2020 09:51 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/12 "2020-09-26T09:51:32Z")

</div>

> [@torrent](#):
>
> Hi, you could try this platform from Docker

Not going there 😛

  

Does my solution work for you?  
Do you understand what the intention is?  
Have you looked in the context viewer to see if there are any values?  
Do you know what flow context is?

Perhaps if you expand the payload object returned from HTTP GET REQ in the debug window and sent a screen shot and copy the output (using the copy button on the debug window) then paste it ...  
`````  
`like this`  
`````  
... i might have a chance of understanding what is returned

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [26 September 2020 10:01 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/13 "2020-09-26T10:01:36Z")

</div>

It doesn't work but I got your idea. Thank you so much. You save my weekend! 😍  
The only thing now is display my object, which we don't know exactly how many objects 🧐

---

<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 September 2020 10:03 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/14 "2020-09-26T10:03:25Z")

</div>

I haven't used it but perhaps the [ui-table node](https://flows.nodered.org/node/node-red-node-ui-table) might be useful.

---

<div class="post-metadata">

**Author:** ![torrent](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@torrent](https://discourse.nodered.org/u/torrent)\
**Post date:** [27 September 2020 12:16 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/15 "2020-09-27T12:16:41Z")

</div>

Thank you, guys. It truely works for me

---

<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:** [26 November 2020 12:16 UTC](https://discourse.nodered.org/t/save-value-from-a-request/33359/16 "2020-11-26T12:16:43Z")

</div>

Closed on 26 November 2020 12:16 UTC

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