So, I am using predefined collection variables in the body. Then I take the request body and convert to string in PRE_REQUEST body.
The problem is that it doesn’t use assigned valued to the variables, but use PLAIN variable names and converts to the string.
Is there any possible way to solve this issue?
P.s. I started assigning manually in pre-request sctipt using variable.get(). But the problem doesn’t get solved for fakers
So, I solved the problem with fakers so I get the same value in pre request and request body>
But still, manually assigning each key to value in pre request is kind of frustrating.
WIthout it and just plain using stringfy on request.body, it gives me plain variable names and not their values.
The variable substitution only happens when the request is run, so when you retrieve the body in a pre-request script, it will just return a string representation.
If you console log the body in the post- response script, then the values will be transposed at this point.
Not sure what is going on with the randomUUID, as this can be called directly within the body.
From what I can tell, the only difference between your initial request body and what happens in the pre-request script is the reason key.
If you just want to add, update or delete a key/value in your body, then you can do the following.
// step 1 - parse existing body
let body = JSON.parse(pm.request.body.raw);
// step 2 - transform existing body
body.reason = "reason"; // update value
delete body.sessionId; // delete key
body.newKey = "newValue" // add key/value
// set new body to be used in request
pm.request.body.raw = body; // you don't have to stringify the body, but you can if you want, both will work