3

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.

Matthew
  • 1,461
  • 3
  • 23
  • 49
  • What does the token response request look like? Not sure why you would need to replace any of that before setting the variable. You're also mixing the older test syntax with the newer set variable syntax - I would recommend refactoring what you have to use all the new `pm. *` functions. – Danny Dainton Oct 01 '18 at 06:38

1 Answers1

3

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

lloyd
  • 1,683
  • 2
  • 19
  • 23
  • I can get this to work when running from the main edit window using global variables, but fails when using the Runner. Any ideas what I might be doing wrong? – Matthew Oct 01 '18 at 15:09
  • 1
    Will you add to your answer that you must select an `Environment` and select `Keep variable values` from the Collection Runner dialog? – Matthew Oct 01 '18 at 16:10
  • @Matthew I've added what you suggested to the answer. Please review and let me know if I missed anything. Thanks. – lloyd Oct 02 '18 at 00:52