# Request.data set not working

**URL:** <https://community.postman.com/t/request-data-set-not-working/19096>\
**Category:** Help Hub\
**Created:** [January 12, 2021, 8:51pm UTC](https://community.postman.com/t/request-data-set-not-working/19096 "2021-01-12T20:51:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![science-physicist-94](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@science-physicist-94](https://community.postman.com/u/science-physicist-94)\
**Post date:** [January 12, 2021, 8:51pm UTC](https://community.postman.com/t/request-data-set-not-working/19096/1 "2021-01-12T20:51:43Z")

</div>

I’ve tried searching forums here and other places, i can’t seem to find anyone having a similar issue.

request.data=[STRING]  
In one context is not updating the payload as expected.

I use this basic structure fairly regularly in relation to hmac generation and variable usage.

var payload= (request.data.length \>0)?request.data:"";  
payload= payload.replace("{{USERNAME}},pm.variqbles.get(USERNAME));  
request.data = payload;

This generally works. I mean it always works in the context of the hmac generation i reference.

However im trying to do the same in a different context, printing payload to the logs i can confirm the string is being updated the way i want but the request.date = payload doesn’t set the body. It just uses what the default was.

Any idea why this would be?

What im trying to achieve is if one variable is one value other fields will be removed from the body. This replace structured seemed like the obvious way to do that but I can’t even force this simplified structure to work.

Any help understanding this will be appreciated. I suspect there’s a gap in my understanding of request.data

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [January 13, 2021, 2:29am UTC](https://community.postman.com/t/request-data-set-not-working/19096/2 "2021-01-13T02:29:01Z")

</div>

Hi @science-physicist-94,

Welcome to the community! 👏

Sounds like there’s a fairly simple solution for your issue. So you can’t actually update the request body that way. It will always use whats in the “Body” section of the Postman Request. However, you can use an environment variable for the body, such as {{body}} or {{payload}}, and in your pre-request script, you can do all the modification you need to the body. Then at the end, set the body variable like so:

`pm.environment.set("payload", payload);`  
or  
`pm.environment.set("body", body);`

Then, you the request body will actually be updated based on your script.

Hope this is what you’re looking for!

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [January 13, 2021, 4:12am UTC](https://community.postman.com/t/request-data-set-not-working/19096/3 "2021-01-13T04:12:48Z")

</div>

Hi @science-physicist-94! Welcome to Postman community 😃

As @odanylewycz suggests, request body cannot be modified in script - here is a similar question discussed: [Update request.body in pre-request script](https://community.postman.com/t/update-request-body-in-pre-request-script/2467)

Hope this helps!
