7

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?

Matthew
  • 1,461
  • 3
  • 23
  • 49

4 Answers4

8

#Example if you setting ApiToken that is dynamic.

  • Under Tests tab in postman.

Put the following code.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Token", jsonData.token);
  • Under specific environment put variable name as "Token" and current value will be set automatically.
  • access the variable using {{variable_name}} #Example: {{token}}
Dev-lop-er
  • 578
  • 1
  • 7
  • 16
6

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.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • OK. Should I be setting a token in the first test script, and then getting in the second requests pre-request script? – Matthew Sep 30 '18 at 23:38
  • 1
    If your first request is to a 'token' endpoint and that is returned in the response. You would need to set that value in the test, so you have it to use in the following requests. – Danny Dainton Oct 01 '18 at 06:33
  • OK. It appears to be setting properly. I can inspect it with the eye icon. I think I am having trouble retrieving the variable in a follow up request and injecting it into the header. – Matthew Oct 01 '18 at 13:51
  • Do I use `let jwt = pm.globals.get("jwt");` in the test section of the follow up request? – Matthew Oct 01 '18 at 14:08
  • Or do I just run it as `pm.globals.get("jwt");`? – Matthew Oct 01 '18 at 14:10
  • 1
    You can just use `{{jwt}}` in the request header. No need to get it that why unless you're using it in a different way. – Danny Dainton Oct 01 '18 at 14:30
  • Hm. I get it to run fine on its own using the `Authorization` tab and setting the token to `{{jwt}}`. However, the test fails when I try using the Runner... – Matthew Oct 01 '18 at 14:58
  • Add a Authorisation request header with a `Bearer {{jwk}}` value. – Danny Dainton Oct 01 '18 at 15:34
  • My answer was based on the current information that you provided in the question. Apart from the tag, there is no mention of even using the runner. You may need to expand on your question so that people are able to give you a better, detailed and more focused answer. Without that, it's just guess work. :) – Danny Dainton Oct 01 '18 at 16:21
  • 1
    Sorry, I was replying to the wrong thread. Thanks for the help!! – Matthew Oct 01 '18 at 16:50
1

This answer by @Dev-lop-er could be written a bit differently as well.

const jsonData = pm.response.json();
/* Set collection variable */
pm.collectionVariables.set("token", jsonData.token);
/* Or set environmental variable */
pm.environment.set("token", jsonData.token);
Paul B
  • 3,989
  • 33
  • 46
0

If you do not have the SDK active (monthly payment required to Postman). You can retrieve the values through __environment and __globals.

//Use of environment
postman.setEnvironmentVariable("my_value", "This is a value");
//...
let myValueVar = postman.__environment["my_value"];
console.log("Variable value is:", myValueVar);
//shows: Variable value is: This is a value

//Use of Globals
postman.setGlobalVariable("my_value2", "This is a value of globals");
//...
let myValueVar2 = postman.__globals["my_value2"];
console.log("Variable value is:", myValueVar2);
//shows: Variable value is: This is a value of globals

Also you can see your globals variables in Environments->Globals of your Posmant client.

elpezganzo
  • 349
  • 4
  • 7