3

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pooja
  • 2,162
  • 5
  • 33
  • 64

2 Answers2

8

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);
Riddhi Sanyal
  • 433
  • 5
  • 7
0

postman.setGlobalVariable("item", Number(postman.getGlobalVariable("item"))+1);

Pooja
  • 2,162
  • 5
  • 33
  • 64