"HTTP Request Node" seems to overwrite all sub-strings in "msg.payload", elegant solution for working around this?

Heya,

somewhat new-ish to Node-RED in general so sorry if this might sound noobish :slight_smile:

In short I'm making a REST API call via the "HTTP Request Node" from a Telegram chat command, the Telegram node appends some sub-strings to "msg.payload" such as "msg.payload.chatId" etc.
I've added the HTTP Request Node after the Telegram receive node, however it seems that once the request node finished doing it's thing it completely wipes the "msg.payload" clean and overwrite whatever was already there. The "msg.payload.chatId" as example is no longer present after it.
I couldn't work around this cause both nodes receive/output as "msg.payload" and I can't seem to change this.

Is this intended behavior? If so, what is a good/elegant way to work around this and "merge" multiple "msg.payload" sub-strings so it becomes one big payload? I tried msg.payload = msg.telegram_node;
but this also overwrites every sub-string.
My current solution is to use the following which works but is rather hardcoded which I don't like too much ^^"

msg.payload.type = msg.telegram_node.type;
msg.payload.chatId = msg.telegram_node.chatId;

My current flow looks like this:

Thanks already for your help!

Hi and welcome.

Prior to http request node move your payload to another msg property, using a change node. You can then after the request recreate the payload you require, using the saved data in the other properties.

1 Like

Move any values that you want retained to different message properties, msg.chatId, msg.type etc.

A well behaved node will pass these message properties through unchanged.

The no-code way to do this is with a change node.

1 Like

Can I somehow "merge" 2 different payloads afterwards without knowing what sub-values either of them have? So, as example:
msg.payload.chatId will be moved to msg.telegram_node.chatId, but meanwhile the HTTP Request generates a msg.payload.version. I later want to combine everything into a single msg.payload with all sub-values from all payloads without knowing what kind of sub-values either the HTTP Request payload or telegram_node has now.

In a function node you could do the following to merge the two objects:

msg.payload = { ...msg.payload, ...msg.telegram_node }
return msg

Note that if both msg.payload and msg.telegram_node have a property with the same name, then the resulting object will only have the value from the telegram_node (because of the order I put them in the statement above).

1 Like

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