# Keeping original payload data while editing it

**URL:** <https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557>\
**Category:** General\
**Tags:** http-in\
**Created:** [12 January 2025 17:11 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557 "2025-01-12T17:11:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:11 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/1 "2025-01-12T17:11:32Z")

</div>

[Flow json](https://pastebin.com/MfupxG84)

```auto
{
  "data": 0
}

```

I feel like I'm trying something really simple and I can't seem to find an answer anywhere.  
I have a simple http request test and all I need it to do it take the request. modify the data and store the original request and the modified data to a file, then have a way to read that file back. I have everything working, but I can't get the original payload data. Please help Thanks

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 January 2025 17:17 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/2 "2025-01-12T17:17:42Z")

</div>

Welcome to the forums @lafoscony

You can deep clone any object before modifying it...  
The best way, is to use the `change` node.

 ![Screenshot 2025-01-12 at 17.16.35](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c32222fa0023cea902df76de8f8e8411cb01965b.png)

This will create an object called `backupPayload` - this will create a **deep** cloned version of `payload`

Meaning edits to `payload` wont effect `backupPayload`  
Place the `change` node before any nodes making changes to the `payload`

Its important to use **Deep clone** - as javascript is `pass-by-ref`, i.e if you don't deep clone, changes to `payload` - will also change `backupPayload`

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:30 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/3 "2025-01-12T17:30:10Z")

</div>

When I do that it causes my function node to no longer work. It seems like the payload object just disappears. That was the first thing I tried, I just can't get it to work

---

<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:** [12 January 2025 17:30 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/4 "2025-01-12T17:30:48Z")

</div>

Just to not the original incoming data is also in `msg.req.body`

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 January 2025 17:31 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/5 "2025-01-12T17:31:33Z")

</div>

The http request will be changing `payload` - that is normal.

so if you function node still needs data you sent.  
change to using `backupPayload` inside off it (as it will be the original `payload`) before the http request

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:31 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/6 "2025-01-12T17:31:53Z")

</div>

When I look there the variable is updated to the new one. I think it's referencing it when I modify it. I'm definitely doing something wrong, but I can't find any good documentation on how to do it

---

<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:** [12 January 2025 17:35 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/7 "2025-01-12T17:35:56Z")

</div>

It is unclear as to what you want to see in the file and what you want to return to the response. please provide examples.

req.body is clearly still there

 ![chrome_screenshot_12 Jan 2025 17_33_09 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c263ae819b4586989b67c40cb556e49613483a3c.png)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 January 2025 17:37 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/8 "2025-01-12T17:37:09Z")

</div>

Have I missed something 🤔

is this a HTTP Request - or HTTP IN?

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:39 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/9 "2025-01-12T17:39:05Z")

</div>

It's an http in. I still can't get it to work with the change node. Even trying to get debug to print msg.backup returns nothing

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:39 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/10 "2025-01-12T17:39:59Z")

</div>

I posted the flow on the original message. Hell I'd even let you login and look at this one. I'm just at a loss. I'm so confused right now

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:42 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/11 "2025-01-12T17:42:12Z")

</div>

I don't know what else to provide. I gave the flow. I gave the original payload. and I said I would like it to just include the original payload with what it's already returning. I don't know how to do that. and when I look at the req data in the body it is the modified data, not the original

---

<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:** [12 January 2025 17:44 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/12 "2025-01-12T17:44:26Z")

</div>

Without examples of the Object return to the response and what Object should go to the file, I am guessing now as I don't really fully get what you are saying.

```auto
let something = {};
something.data = msg.payload.data;

something.data += 1

msg.payload.body = {
        sourceSummary: something.data,
        errorNumbers: "errorNumbers",
        missingNumbers: "missingNumbers"
};

return msg;

```

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 17:48 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/13 "2025-01-12T17:48:56Z")

</div>

I'm sorry I don't know how to explain any better. I send a post with  
{ "data": 0 } I need to modify data lets say I just need to add one to it. I need to save to a file  
{ "data": 0 } (the original request) and { sourceSummary: 1(data +1), errorNumbers: "errorNumbers", missingNumbers: "missingNumbers" }

---

<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:** [12 January 2025 17:52 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/14 "2025-01-12T17:52:21Z")

</div>

Isn't that what my code above does?  
output

```auto
{
  "data":0,
  "body":{ 
    "sourceSummary":1,
    "errorNumbers":"errorNumbers",
    "missingNumbers":"missingNumbers"
  }
}

```

you can then move delete any property at will.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 January 2025 17:54 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/15 "2025-01-12T17:54:30Z")

</div>

Ok...

You're referring to `HTTP Request` (that is a Node) - hence my confusion 🤪  
(but I didn't have a look at the flow to be fair - so that is on me)

Directly after the `HTTP IN` node

- Use a `change` node to store `msg.payload` in `msg.original` (use **deep** clone)
- After you make changes to `payload` (for the client)
  - Respond (to client)

- Then to store the original incoming data, and response to a file (after you respond)
- use something like below

```auto
const Log = {
  request: msg.original,
  response: msg.payload
}

return {payload:JSON.stringify(Log)}

```

Send the above `function` to the file wrote node?

Insurance: I still may have this wrong

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 18:01 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/16 "2025-01-12T18:01:37Z")

</div>

It does do what I need, I just need it to only happen to the file that's being written, not in the actual response data itself

---

<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:** [12 January 2025 18:07 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/17 "2025-01-12T18:07:19Z")

</div>

So i asked what the response and file should be several posts ago.

So what should go to response?  
and what should go to file?

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 18:07 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/18 "2025-01-12T18:07:29Z")

</div>

Any use of the change node makes it where I can't access original payload, or what i've changed it to

---

<div class="post-metadata">

**Author:** ![lafoscony](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@lafoscony](https://discourse.nodered.org/u/lafoscony)\
**Post date:** [12 January 2025 18:12 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/19 "2025-01-12T18:12:12Z")

</div>

![node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f163fa3c1435ce1f40fc20d209bf14b13b5266a7.jpeg)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 January 2025 18:12 UTC](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557/20 "2025-01-12T18:12:39Z")

</div>

Because you have it the wrong way  
you are setting `payload`

[Next page](https://discourse.nodered.org/t/keeping-original-payload-data-while-editing-it/94557.md?page=2)
