Doing http request with every msg.payload.id

#1

Hello!

I'm new to using Node-Red and solving a problem step by step.

I'm trying to do Rest API query for each ID that i've got in "msg.payload.id". For example https://website.com/api/<msg.payload.id>.

At the moment i'm getting all ID-s, which are available with http.request node, then I'm using "change" node to move payload.items to payload. Then I'm using split to break up array. And finally sort to select only those I'm interested in.

And now i'm stuck.

What is the best way to take every objects' unique ID and running http query with them? Setting a global variable won't help. If I could use http request in function node, then a simple for cycle would help.

0 Likes

#2

The HTTP Request node can be used to make the request. It lets you specify the url to request by passing in msg.url.

Does that help?

0 Likes

#3

So if i would make a function node, where I set url + id to msq.url to make query with http.request.node, will it run with every object automatically and I don't have to worry about using for cycles?

at the moment my last node, which is sort, outputs 30 msg.payload objects, which all have unique id's

0 Likes

#4

If you have an array in msg.payload then you can feed that through a Split node and you will get a sequence of messages, one for each element of the array. You can then feed those into the http node.

0 Likes

#5

If you just want to substitute fields from the incoming msg object into the url, you can use "mustache" syntax -- as mentioned in the http request node's info panel:

When configured within the node, the URL property can contain mustache-style tags. These allow the url to be constructed using values of the incoming message. For example, if the url is set to example.com/{{{topic}}} , it will have the value of msg.topic automatically inserted. Using {{{...}}} prevents mustache from escaping characters like / & etc.

I'm doing this same thing right now... read in records from a csv file, split into individual msg objects, and set my url to use syntax like https://{{{myserver}}}/api/users/{{payload.username}}

The nice thing with this is that I can dynamically build any url string, even setting the target hostname, from the data i send in the msg. Note that I use the double braces on the incoming username so it gets url encoded properly, which I don't need with the server information...

0 Likes

#6

Thanks alot everybody for helping me to solve the problem!

Using mustache-style tags solved linking ID with URL using http request node. Colin answer made it clear I can just use split node and then using http request after rather than the need to use function node between.

0 Likes