Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update global variable in Postman?

Tags:

postman

I have a test which Posts some items
e.g.
POST:item : 1
POST:item : 2 and so on

and now in 1st Post request, I am trying to set a global variable in pre-request-script

postman.setGlobalVariable("item", 1);

and use this variable in the body e.g

 "item": "{{item}}",

it work.
Now in 2nd Post request, I wanted to increment global variable, in pre-request-script

item=item+1;
postman.setGlobalVariable("item", item);

and in body same as above. but its give following error

There was an error in evaluating the Pre-request script: item is not defined

like image 808
Pooja Avatar asked Aug 17 '26 19:08

Pooja


1 Answers

With the current version of Postman (6.2.x), you can set the variables in the Tests tab like

Global Variables

pm.globals.set("variable_key", "variable_value");

Environment Variables

pm.environment.set("variable_key", "variable_value");

To update the answer, it will be

pm.globals.set("item", Number(postman.getGlobalVariable("item"))+1);
like image 166
Riddhi Sanyal Avatar answered Aug 20 '26 12:08

Riddhi Sanyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!