testingautomated-testspostmanpostman-collection-runner

How to Set Variable from Request in Postman


I am trying to write some tests for Postman. Many of the requests require an API key that gets returned by an initial GET request.

To set something hard-coded that is not dynamic, it looks like the test code is of the form

let variable = pm.iterationData.get("variable");
console.log("Variable will be set to", variable);

How do I set a return value as a global variable and then set that as a header parameter?


Solution

  • You can specify that variable value in the request Headers by using the {{var_name}} syntax. Instead of any hardcoded value that you may have been using.

    You would previously have had to set the value using the pm.globals.set()syntax.