# Accessing request.data with filled body parameters in pre-request script

**URL:** <https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554>\
**Category:** Help Hub\
**Created:** [December 11, 2019, 7:20pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554 "2019-12-11T19:20:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![deveric](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deveric](https://community.postman.com/u/deveric)\
**Post date:** [December 11, 2019, 7:20pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/1 "2019-12-11T19:20:37Z")

</div>

I have the following request body:

```
{
    "data": [
        {
            "productId": "{{productId}}",
            "quantity": 2,
            "attributes": {
                "startAt": "2019-12-13T17:58:44.000Z",
                "endAt": "2019-12-30T17:59:44.000Z"
            }
        }
    ]
}

```

In a pre-request script, I want to access this content… but with the ‘productId’ populated. Right now, when I attempt to access it (using request.data or pm.request.body) I get back the raw content with that actual “{{productId}}” template tag and not the value that should be inserted into it.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 11, 2019, 8:42pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/2 "2019-12-11T20:42:06Z")

</div>

If you’re in the pre-request script then you’re going to have to use the js client to resolve your variables.

```javascript
let productId = pm.environment.get("productId");

```

---

<div class="post-metadata">

**Author:** ![deveric](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deveric](https://community.postman.com/u/deveric)\
**Post date:** [December 11, 2019, 8:59pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/3 "2019-12-11T20:59:11Z")

</div>

Allen,

I can get the variable from the environment, no issues. What I need is the entire request body with the {{productId}} swapped out with that value. Whether I can do it with a simple command, or use some kind of replace function to replace {{productId}} with what’s returned with pm.environment.get(“productId”). What I’m trying to do is create a hash that is used for authorization from the request payload. Since the actual payload going to the end-user uses the actual product ID and not “{{productId}}”, the hash that I create is wrong. Hope that makes sense. If not, let me know so I can clarify further.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 11, 2019, 10:19pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/4 "2019-12-11T22:19:27Z")

</div>

So maybe some simple javascript to do what you want?

```auto
let productId = pm.environment.get("productId");
let requestBody = JSON.parse(<enter your request body here>);
for(let i = 0; i < requestBody.data.length; i++){
  requestBody.data[i].productId = productId;
}

```

---

<div class="post-metadata">

**Author:** ![deveric](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deveric](https://community.postman.com/u/deveric)\
**Post date:** [December 12, 2019, 12:28am UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/5 "2019-12-12T00:28:21Z")

</div>

That worked… appreciate it!

---

<div class="post-metadata">

**Author:** ![onishi.sayuri](https://avatars.discourse-cdn.com/v4/letter/o/9d8465/32.png) [@onishi.sayuri](https://community.postman.com/u/onishi.sayuri)\
**Post date:** [October 1, 2020, 10:29am UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/6 "2020-10-01T10:29:23Z")

</div>

just sharing what I have found out today 🙂  
you could try this  
requestBody = pm.variables.replaceIn(pm.request.body.raw);

---

<div class="post-metadata">

**Author:** ![gimmemore](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gimmemore/32/12814_2.png) [@gimmemore](https://community.postman.com/u/gimmemore)\
**Post date:** [August 3, 2022, 8:25pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/7 "2022-08-03T20:25:46Z")

</div>

> [@onishi.sayuri](#):
>
> requestBody = pm.variables.replaceIn(pm.request.body.raw);

Hi @allen.helton ,

i got same issue like this, but i store the value in testData. how to populate it when i do run data driven through collection ? can you help?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 7, 2022, 4:46pm UTC](https://community.postman.com/t/accessing-request-data-with-filled-body-parameters-in-pre-request-script/9554/8 "2022-09-07T16:46:58Z")

</div>

Hey @gimmemore, would you please provide some more information there?

So you’re running a collection using iteration data and you’re looking to replace some parameterized values? Would you mind adding some screenshots please?
