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.