How to Set Variable from Request in Postman
Asked Answered
S

4

9

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?

Situla answered 30/9, 2018 at 22:25 Comment(0)
P
7

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.

Philoctetes answered 30/9, 2018 at 23:35 Comment(10)
OK. Should I be setting a token in the first test script, and then getting in the second requests pre-request script?Situla
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.Philoctetes
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.Situla
Do I use let jwt = pm.globals.get("jwt"); in the test section of the follow up request?Situla
Or do I just run it as pm.globals.get("jwt");?Situla
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.Philoctetes
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...Situla
Add a Authorisation request header with a Bearer {{jwk}} value.Philoctetes
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. :)Philoctetes
Sorry, I was replying to the wrong thread. Thanks for the help!!Situla
G
12

#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}}
Greenquist answered 15/7, 2020 at 10:28 Comment(0)
P
7

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.

Philoctetes answered 30/9, 2018 at 23:35 Comment(10)
OK. Should I be setting a token in the first test script, and then getting in the second requests pre-request script?Situla
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.Philoctetes
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.Situla
Do I use let jwt = pm.globals.get("jwt"); in the test section of the follow up request?Situla
Or do I just run it as pm.globals.get("jwt");?Situla
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.Philoctetes
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...Situla
Add a Authorisation request header with a Bearer {{jwk}} value.Philoctetes
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. :)Philoctetes
Sorry, I was replying to the wrong thread. Thanks for the help!!Situla
B
5

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);
Bivalve answered 13/7, 2023 at 14:10 Comment(0)
A
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.

Astrix answered 14/9, 2022 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.