# Change request body in pre-request script

**URL:** <https://community.postman.com/t/change-request-body-in-pre-request-script/24566>\
**Category:** Help Hub\
**Tags:** pre-request\
**Created:** [June 7, 2021, 2:52am UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566 "2021-06-07T02:52:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![docking-module-geol4](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@docking-module-geol4](https://community.postman.com/u/docking-module-geol4)\
**Post date:** [June 7, 2021, 2:52am UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/1 "2021-06-07T02:52:10Z")

</div>

I am doing body level encryption in the pre-request script and want to the replace the entire reuqest body with encrypted data before sending the actual request

---

<div class="post-metadata">

**Author:** ![meenakshi.dhanani](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meenakshi.dhanani/32/5980_2.png) [@meenakshi.dhanani](https://community.postman.com/u/meenakshi.dhanani)\
**Post date:** [June 7, 2021, 6:15am UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/2 "2021-06-07T06:15:36Z")

</div>

Hey @docking-module-geol4,  
Welcome to the community 👋  
Is there anything, a question, or probably a hint as to where you’re trying to drive this discussion? Are you facing any issues with the pre-request script? We have some wonderful folks in the community who can help but make sure you mention what it is that you’re looking for.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [June 7, 2021, 11:29pm UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/3 "2021-06-07T23:29:59Z")

</div>

just provide a variable in place of request body:

`{{requestBody}}`

And modify this from pre-request script

`pm.variable.set("requestBody", JSON.stringify( { "name":"test"} ))`

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [June 7, 2021, 11:31pm UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/4 "2021-06-07T23:31:56Z")

</div>

you can also modify it as 🥰

`pm.request.body='{"name":"age"}'`

Let us if you would need any help 😃

---

<div class="post-metadata">

**Author:** ![brunovivaldodev](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@brunovivaldodev](https://community.postman.com/u/brunovivaldodev)\
**Post date:** [April 1, 2023, 10:31am UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/5 "2023-04-01T10:31:55Z")

</div>

I tried this and it didn’t work, instead this works

pm.request.body.raw = { name: “age” }

---

<div class="post-metadata">

**Author:** ![mission-technologis9](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mission-technologis9](https://community.postman.com/u/mission-technologis9)\
**Post date:** [June 23, 2023, 10:14am UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/6 "2023-06-23T10:14:33Z")

</div>

I want to append some items in the request body.

I tried this but not working

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 26, 2023, 11:48am UTC](https://community.postman.com/t/change-request-body-in-pre-request-script/24566/7 "2023-06-26T11:48:20Z")

</div>

@mission-technologis9

How did you try this?

Can you provide a screenshot?

The above examples are replacing the body.

If you want to append, you can do it using the following method.

```auto
const body = JSON.parse(pm.request.body.raw); // retrieve current body
// update the body\object as appropriate
pm.request.body.raw = body; // write back updated body

```
