Using predefined variables in the pre-request scripts

Hello.

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 :frowning:

Thanks!

Hey @tvittamar :wave:t2:

Can you share an example of what you’re request body and the script look like, please?

It’s going to be easier to see what you have and what you’re trying to do.

Request Body:

{
  "sessionId": "{{sessionId}}",
  "siteId": "{{siteId}}",
  "thisId": "{{thisId}}",
  "currency": "USD",
  "randomizer": "{{$randomUUID}}",
  "exampleName": "name",
  "amount": "20",
  "reason": ""
}

pre-request Script:

let randomUUID = pm.variables.replaceIn('{{$randomUUID}}');

let requestObj = pm.request.body.raw = JSON.stringify({
  sessionId: pm.collectionVariables.get("sessionId"),
  siteId: pm.collectionVariables.get("siteId"),
  thisId: pm.environment.get("thisId"),
  currency: "USD",
  randomizer: randomUUID,
  exampleName: "name",
  amount: "20",
  reason: "reason"
})

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.

I don’t know the full context or why you have done it this way but couldn’t you also just do something like this:

Request Body

{{requestObj}}

Pre-request Script

let requestObj = {
  sessionId: pm.collectionVariables.get("sessionId"),
  siteId: pm.collectionVariables.get("siteId"),
  thisId: pm.environment.get("thisId"),
  currency: "USD",
  randomizer: pm.variables.replaceIn('{{$randomUUID}}'),
  exampleName: "name",
  amount: "20",
  reason: "reason"
}

pm.variables.set("requestObj", JSON.stringify(requestObj));

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.

{
  "sessionId": "{{sessionId}}",
  "siteId": "{{siteId}}",
  "thisId": "{{thisId}}",
  "currency": "USD",
  "randomizer": "{{$randomUUID}}",
  "exampleName": "name",
  "amount": "20",
  "reason": ""
}

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
2 Likes

Thank you. Didn’t know variables required to run to get a proper value.
And about the reason key, I just forgot to assign the value :smiley:

1 Like

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