0
votes

In Postman is it possible to use the same integer value on the same request
and increase it between requests sent?

for example first request:

 {
  "country": "US",
  "date": "",
  "printerType": "Xerox_1",
  "printerID": "TEST_1",
  "printerLocation":"LOCATION_1",
  "printerName":"PRINTER_NAME_1",
  "printerStatus":"PRINTER_STATUS_1"
}
  

second request:

{
  "country": "US",
  "date": "",
  "printerType": "Xerox_2",
  "printerID": "TEST_2",
  "printerLocation":"LOCATION_2",
  "printerName":"PRINTER_NAME_2",
  "printerStatus":"PRINTER_STATUS_2"
}

I found a way to use random integers but they change across the body, Im also not sure how to increase the value between submissions.

enter image description here

1
You could use the pre-request the set the int value and use it in the body. At the same time, set it in an environment variable then +1 the value each time the request is run. - Danny Dainton
Can please you show me an example? - JavaSheriff
Not at my laptop at the moment but this answer could be adapted to suit what you need. Forget the setNextRequest part but look at the counter. Have that first bit that gets the initial value in a pre-request, set that variable in your boby and then the second part that increases the number by 1 in the tests community.postman.com/t/… - Danny Dainton

1 Answers

1
votes

You can solve this via an environment variable index (or whatever name you prefer) in the pre-request script.

If the environment variable doesn't exist, it's being set to 1. If it does exist, it's being increased by 1.

if (!pm.environment.get("index")) {
    pm.environment.set("index", 1);
} else {
    let index = parseInt(pm.environment.get("index"));
    index++;
    pm.environment.set("index", index);
}

In the body you then appen {{index}} to the static values:

{
   "country":"US",
   "date":"",
   "printerType":"Xerox_{{index}}",
   "printerID":"TEST_{{index}}",
   "printerLocation":"LOCATION_{{index}}",
   "printerName":"PRINTER_NAME_{{index}}",
   "printerStatus":"PRINTER_STATUS_{{index}}"
}

For that you need to have an environment created and selected (no need to add any variable manually). How to do that is described here: https://learning.postman.com/docs/sending-requests/managing-environments/#creating-environments