Checking dynamic variables in pre-request script

Hello

I need to prepare Pre-request script. In my request i have dynamic Json body

{
    "name": {{name}},
    "template": {{template}}
}

This dynamic values are loaded from json file. I need to check in pre-request script vaule of curren request variable. How can I do that?

Hey @rafall92

If I understand you correctly, you should be able to use this to log the name variable to the console:

console.log(pm.iterationData.get('name'));

More about iterationData can be found here:

Yes, that’s what i’m looking for. Technically I need that to check if data already exists(name and template are send via post request) In pre-request i write script, where I send Get request to recive all data from my Api, ant then compare it with one i obtain with iterationData.get. Is that right approach or can I do it better?

Hi @rafall92

If you want to have it determined if you need to run your Request, what I usually end up doing (not sure if there is a better way) is to create a Request that runs before the request that you use.

Example:

Request 1 (“DataCheck”):
No Pre-Request script
Have request use Get, if you have an endpoint that is a ping response, use that
Tests
if(pm.iterationData.get(‘name’) == “”) {
postman.setNextRequest(null);
} else {
postman.setNextRequest(“MyRequest”);
}

Request 2 (“MyRequest”):
*This is your script

Hope this helps!