# How can I override a request value when req body is a variable?

**URL:** <https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517>\
**Category:** Help Hub\
**Tags:** variables, collections, request, pre-request-script\
**Created:** [July 24, 2024, 7:35pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517 "2024-07-24T19:35:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robjet2](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@robjet2](https://community.postman.com/u/robjet2)\
**Post date:** [July 24, 2024, 7:35pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517/1 "2024-07-24T19:35:37Z")

</div>

I have searched already on other topics, and the closest I have found is to use replaceIn - which I have tried and can’t get it to work. It could be me doing it wrong I am still very much a newbie. I have also got a solution that does work, but doesn’t feel quite right.

I am setting a collection level variable for me POST Request as a pre request script, then I am calling this in the POST request body itself - this works if I do not use other variables and try to replace/override them as part of the request pre request scripts.

My issue comes when I try to replace a single value within the request.

**Question**  
How can I override/replace a single value in a request body that has been set as a variable at collection level.

All requests are changed for privacy.

**Request set as a collection level variable changed for privacy**

```auto
var configRequestBody= {
    "hotelConfig": [
        {
            "locationIds": [
                440
            ],
            "configuration": {
                "sMA": pm.collectionVariables.get('sMA') 
                //set to 3 for the example,                  
                "lMA": 7,
                "mIT": 1.00,
                "mIIP": 1.00,
                "rPID": 2,
                "rOSN": 7
            }
        }
    ]
};

var configRequestBody_str = (JSON.stringify(configRequestBody));

pm.collectionVariables.set('configRequestBody_str', configRequestBody_str);

```

**POST Request variable being called**

```auto
{{ConfigRequestBody_str}}

```

**Post Request Pre Request Script**

```auto
pm.collectionVariables.set('sMA', 5);

pm.collectionVariables.get('sMA');

```

This works if I do a SET and then a GET within the request Pre Request Scripts. Is there a better way to do this as I’d hoped I wouldn’t have to use pm.collectionVariables.Get(‘sMA’) after setting to make it work.

I would be looking to do with these for each of the values within the request.

I have a fair few post requests with pretty much the same request body barring the values, so looking to reduce the size of the file really, and this was a way I came up with to try reduce it.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [July 25, 2024, 12:12pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517/2 "2024-07-25T12:12:22Z")

</div>

Hey @robjet2 👋

Welcome to the Postman Community! :postman:

This is working for me, I changed a couple of thing like wrapped the variable in `parseInt` so that it’s a number (following the convention of the others) and changed it from a `pm.collectionVariables.set()` to `pm.variables.set()` so that it only using that at runtime and doesn’t store it and cause other variable scope relate issues.

```auto
var configRequestBody= {
    "hotelConfig": [
        {
            "locationIds": [
                440
            ],
            "configuration": {
                "sMA": parseInt(pm.collectionVariables.get('sMA')),
                //set to 3 for the example,                  
                "lMA": 7,
                "mIT": 1.00,
                "mIIP": 1.00,
                "rPID": 2,
                "rOSN": 7
            }
        }
    ]
};

var configRequestBody_str = JSON.stringify(configRequestBody);

pm.variables.set('configRequestBody_str', configRequestBody_str);

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/c/dcac38b12ff2e6330116417fdc3965966974230c.png)

If this is not what you’re looking for I can take another look if you provide some further guidance.

---

<div class="post-metadata">

**Author:** ![robjet2](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@robjet2](https://community.postman.com/u/robjet2)\
**Post date:** [July 25, 2024, 1:54pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517/3 "2024-07-25T13:54:34Z")

</div>

Thanks for your input @danny-dainton.

Thanks for the parseInt guidance, I came across that previously when trying to run things in Newman.

I guess what I am asking is, is this the best way to do what I was trying to achieve?

Maybe i am overthinking things.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [July 25, 2024, 2:25pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517/4 "2024-07-25T14:25:22Z")

</div>

Do you need to set them at the Collection level?

Could you create a datafile with the values required for each request? Then all you would be doing is referencing those in the request payload, rather than creating the payload in the pre-request script.

---

<div class="post-metadata">

**Author:** ![robjet2](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@robjet2](https://community.postman.com/u/robjet2)\
**Post date:** [July 25, 2024, 3:42pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517/5 "2024-07-25T15:42:57Z")

</div>

That is a very good question. I would have to go away think about using a csv file and maybe figure out if I could make that work with the field and request validation testings I am doing.

Currently I have POST, GET and DELETE requests working in conjunction together so that they flow and validate after each request has run. And the flows throughout the entire collection unless I am doing an update to existing data which is handled slightly differently. e.g.,

- I have a DELETE so I clear and entry in the DB
- POST of the data I want that in this example suceeds
- GET to assert that it has what I expect from the POST.

I will certainly look into your suggestion - thank you.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 24, 2024, 3:43pm UTC](https://community.postman.com/t/how-can-i-override-a-request-value-when-req-body-is-a-variable/64517/6 "2024-08-24T15:43:28Z")

</div>

Closed on August 24, 2024, 3:43pm UTC

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