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

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

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

{{ConfigRequestBody_str}}

Post Request Pre Request Script

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.

Hey @robjet2 :wave:

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.

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);

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

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.

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.

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.

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