IF Statement in Pre Request Script for and Environment

My question

Hi, Iā€™m wondering if itā€™s possible to use an IF statement for an Environment in a Pre Request Script? The reasons for this, is when my GET query is ran, itā€™s pre populated with an id to search for based on an environment, which canā€™t be changed by me as itā€™s in use from other users.

So what Iā€™m wanting to do is, have an IF statement, where when a specific Environment is chosen, the query param fields are pre populated with what i determine in the Pre Request script. I had a stab at writing something to see if it would work, and it didnā€™t.

What i currently have:

if (environment === ā€œExample Environmentā€) {
console.log(ā€œExample Environmentā€)
pm.request.url.query.add({key: ā€˜idā€™, value: ā€˜7790ā€™})
}

If this isnā€™t entirely clear on what Iā€™m after please let me know! Postman is new to me as is writing code.

Postman JavaScript reference | using-environment-variables-in-scripts

const currentEnvironment = pm.environment.name;

if (currentEnvironment === "Example Environment"){  // case sensitive
    console.log("Example Environment")
    // do something
} else {
    // do something else
}

If you are new to Postman I would recommend the Postman training links which are located under ā€œother resourcesā€ in the Learning Centre.

Other resources | Postman Learning Center

The ā€œGalaxy APIā€™s 101ā€ course gets you used to sending requests and the GUI.

The ā€œGalaxy Testing and Automationā€ gets you used to testing your responses and using variables, basic scripting, etc.

Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. WC3 schools is a good place to start.

1 Like