testingautomated-testspostmanpostman-collection-runner

Can Postman Variables be Passed Into Header?


I am trying to string a few Postman requests together for testing.

  1. In the first request I set a global variable as a test script.

    tests['Status code is 200'] = (responseCode.code === 200);
      if (responseCode.code === 200) {
      try {
        let jwt = responseBody.replace(/"/g, '');
        pm.globals.set("jwt", jwt);
        console.log("Variable will be set to", jwt);
      }
      catch(e) {
        console.log(e);
      }
    }
    
  2. In the second request I run a pre-request script as

    let jwt = pm.globals.get("jwt");
    

Then I try to pass it into the header

enter image description here

Is it possible to pass a value into the header when running tests in the runner?

When running tests in the Runner the second request fails due to having an invalid jwt, and the Postman docs only show examples passing variables into the URL.


Solution

  • It's covered in postman auth.

    1. Authenticate to get the JWT(oken) - Token API request
    2. Add the test in to capture the token

      var jsonData = JSON.parse(responseBody);

      postman.setEnvironmentVariable("jwt", jsonData.token);

    3. Authorization > Type > Bearer Token

    4. Token: {{jwt}}
    5. Setup your Environment
    6. Select the Environment
    7. Select Keep variable values from the Collection Runner dialog (if you are running it in command line)

    Note: I'm using version 6.3.0.

    Bearer Token Auth