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