Run request many times with different query params as variable

Hey guys. I want to run one request with different values of query which was set as variables. For example, I have to variable (query params) {{from}} and {{to}} I expect: from = [“2020-12-08”,“2020-12-09”,“2020-12-13”] and same for ‘to’ : [“2020-12-08”,“2020-12-09”,“2020-12-13”] . And I want to execute request many times. It will be three tests for 1 request with different values:
{{baseUrl}}/ui/?dateFromStr=2020-12-08&dateToStr=2020-12-08
{{baseUrl}}/ui/?dateFromStr=2020-12-09&dateToStr=2020-07-09
{{baseUrl}}/ui/?dateFromStr=2020-12-13&dateToStr=2020-07-13

I did this , but in console I see null for parametrs : from and to.
/uuu?dateFromStr=&dateToStr=
UPD: When I added values for variables and now in console :
?dateFromStr=[%222020-07-12%22,%222020-07-15%22]&dateToStr=[%222020-07-12%22,%222020-07-15%22]

Hey @drunyak

The sendNextRequest should be the name of the saved request and not the URL like you have it. Unless that is the actual name of your request :grin:

To loop over that same request, you could use postman.setNextRequest(pm.info.requestName)

It looks like the rest of the code has been taken from a different usecase and adapted with your variable names to suit your needs. I haven’t run this locally so I don’t know what’s going on just by looking at it.

I would suggest that if you have any code in your examples, paste the raw code in a code block rather than an image of the code.

This makes it easier for folks to copy and paste it into thier own Postman instance, you can’t do that with a picture. :grin:

Okay. In pre-request tab:

var from = pm.environment.get("from");
var to = pm.environment.get("to");
// if(!from){
// from = ["2020-07-12","2020-07-15"];
// to = ["2020-07-12","2020-07-15"];}
var currentFromDate = from.shift();
pm.environment.set("fd",currentFromDate);
pm.environment.set("from",from);
var currentToDate = to.shift();
pm.environment.set("td",currentToDate);
pm.environment.set("to",to);

And Test tab:

var to = pm.environment.get("to")
if(from && from.length > 0)
{
    postman.setNextRequest(pm.info.requestName);
}
else{
    postman.setNextRequest(null);
}

Where are you using the fd and td variables?

If you’re only iterating over a set of known values for the two dates, could you just add them to a data file and use that in the runner to resolve those variables in the URL.

It would remove the need to shift those values in the script.

We use AWS pipeline (Newman) I don’t run this request via Postman in Collection Runner. If as you wrote about data file it possible for my case run via pipeline (Newman) so can you tell me how I can do this

You can use the same datafile with your newman command.

Just use -d and the path to the file.

It can be a static file or a public URL that the file data can accessed from in the correct format.

How I can implement this in code?

How are you running Newman now?

We have zero context about how you’re running what you have now so that’s not a question that anyone can answer unless you share that information.

As and everybody. Just give me example for your solution. Please note I use GET method

You can get the command from the Newman document, it’s basically what I mentioned, use the -d flag and specify the path to the data file.

You said about using AWS pipeline to run Newman, what does that look like? Is the Newman command part of a config yml file?

Yes, we install and run Newman in yml. But I more want to know how I can implement this in code in Test tab and Pre-request. I mean this

Implement which part?

Using the data file with Newman or using the code that you have found and tried to use within your collection?

The data file can be referenced in the Newman command, on each iteration it will resolve the variables in the Collection, if these matched the key/column header in the file.

You wouldn’t need any of that code in the scripts.

Did you write that code from scratch and know exactly what each part of that is doing?

I don’t understand you… Okay can you give me example how I have to record values in json file. I have GET method! Where me need add file??? I saw many examples with file but every has POST and in body they select file. Hey

It doesn’t matter what method the request is - the values from the datafile will resolve the variables in the Collection file that use the {{..}} syntax - These could be anywhere in the Collection (URL, params, headers, request body, etc).

Using data files:

https://learning.postman.com/docs/running-collections/working-with-data-files/

The format of the JSON file:

https://assets.postman.com/postman-docs/58702589.json

Okay but How they know that they have to set on place {{from}} and {{to}} variables?
And in your example above they set count of Iteration in Collection runner, but I told you , that I don’t use Postman

If you read the articles from the learning center it will show you.

It doesn’t matter if you use the runner or Newman, it will work in the same way if you use the -d and reference the data file in the Newman command.

You could just try it and and see what it does. :grin:

Okay I’ll try and response here

The iteration count is automatically taken from the number of items in the data file.

If you have 1 item it will run once, if you have 15 items it will run 15 times. Etc.

There seems to be a lot of frustration from your part on this, if you just actually use the data file with Newman and see it for yourself, things will be a lot clearer.

What I have to write here (bold text) : ui/community/overview?dateFromStr={{from}}&dateToStr={{to}}

Creating a JSON file like this and using it with Newman, would 2 iterations and use the values from each object on each run.

[{
  "from": "01/04/2020",
  "to": "01/05/2020"
}, {
  "from": "01/05/2020",
  "to": "01/06/2020"
}]