How do I send a request multiple times while only changing the parameters in POSTMAN?

Viewed 144

I'm very new to postman so please bear with me. Basically, I am trying to get data from the clinicaltrials.gov API, which can only give me 1000 studies at a time. Since the data I need is about 25000 studies, I'm querying it based on dates. So, is there any way in Postman that I can GET multiple requests at one time wherein I am only changing one parameter?

Here is my URL: ClinicalTrials.gov/api/query/study_fields??expr=AREA[LocationCountry]United States AND AREA[StudyFirstPostDate]RANGE[MIN,01/01/2017] AND AREA[OverallStatus]Recruiting

I will only be changing the RANGE field in each request but I do not want to manually change it every time. So, is there any other way in which I can maybe at a list of dates and have Postman go through them all?

1 Answers

There's several ways to do this.

So, is there any way in Postman that I can GET multiple requests at one time wherein I am only changing one parameter?

I'm going to assume you don't mind if the requests are sequenced or parallel, the latter is less trivial and doesn't seem to add more value to you. So I'll focus on the following problem statement.

We want to retrieve multiple pages of a resource, where the cursor is StudyFirstPostDate. On each page retrieved the cursor should increment to the latest date from the previous poge. The following is just one way to code this, but the building blocks are:

  1. You have a collection with a single request, the GET described above
  2. We will have a pre-request script that will read a collection variable with the next StudyFirstPostDate
  3. We will have a test script (post-request) that will re-set the StudyFirstPostDate to the next value of the pagination.
  4. On the test script you should save the data the same way you're doing now.
  5. You set the next request (postman.setNextRequest("NAMEOFREQUEST")) to the same GET request we're dealing with, to effectively create a loop. When you've retrieved all pages you kill the looip with postman.setNextRequest(null) - although not calling any function should also stop it. This then goes to step (2) and loop.

This flow will only work on a collection run. Even if you code all of this, just triggering the request by itself will not initiate a loop. setNextRequest only works within a collection run.

Setting a initial value to the variable on the pre-request script

// Set the initial value on the collection variables
// You could use global or environment variables, up to you.
const startDate = pm.collectionVariables.get("startDate")

Re-setting the value on the Tests

  // Loop through the results save the data and retrieve the next start date for the request
  // After you have it
  const startDate = pm.collectionVariables.set("startDate",variableWithDate)
    
  // If you've reach the end you stop, if not you call the same request to loop
  // nextPage is an example of a boolean that you've set before
  if (nextPage) {
    postman.setNextRequest("NAMEOFREQUEST")
  } else
    postman.setNextRequest(null)
  }
Related