Running a query multiple times against a list of coordinates

First part I’ve managed to do, see my first response to this post.

Apologies if this question is a duplicate, I searched for similar posts but couldn’t find a clear answer.

I have a request which uses latitude and longitude parameters to return nearby assets, and I’d like to run the request against a csv of pairs of coordinates (around 100 or so pairs).

e.g. https://www.server.com/asset_list/query?longitude=123.123&latitude=123.123&search_distance=5

How do I go about setting up the pre-request script? Do I need to add all of the lat/longs as separate arrays? Do I even need to fiddle with the pre-request script?

Is it also possible to download the resulting json output per iteration? Or even merge all of the responses into one json array and then download that?

Greatly appreciate any pointers or assistance!

Aha! I’ve figured out the first part via this helpful video.

Setting the latitude and longitude as variables with identical fields in the csv file, and using the Runner to go through each row is a lot easier than I thought it would be. I like this program already.

Just have to figure out how to download the responses as it loops through now!

1 Like
  • One way to run a query multiple times against a list of coordinates is to use a loop. A loop is a programming construct that allows you to repeat a block of code a certain number of times. In this case, you would want to loop through the list of coordinates and run the query for each coordinate.

Here is an example of how you could use a loop to run a query multiple times against a list of coordinates:
Python

def run_query(coordinate):
  """Runs a query against the given coordinate."""
  query = """
  SELECT *
  FROM table
  WHERE coordinates = %s
  """
  cursor.execute(query, (coordinate,))
  results = cursor.fetchall()
  return results

coordinates = [
  (1, 2),
  (3, 4),
  (5, 6),
]

for coordinate in coordinates:
  results = run_query(coordinate)
  print(results)
  • Another way to run a query multiple times against a list of coordinates is to use a map function. A map function is a function that takes a list of values and applies a function to each value. In this case, you would want to use a map function to apply the run_query function to each coordinate in the list.

`Pythondef run_query(coordinate):
“”“Runs a query against the given coordinate.”“”
query = “”"
SELECT *
FROM table
WHERE coordinates = %s
“”"
cursor.execute(query, (coordinate,))
results = cursor.fetchall()
return results

coordinates = [
(1, 2),
(3, 4),
(5, 6),
]

results = map(run_query, coordinates)
for result in results:
print(result)`

@jamiecole

What on earth does that have to do with the question.

Postman uses JavaScript under the hood, not Python.

Did you just search this on ChatGPT or something?

@southbound

It looks like you have a method for looping through the coordinates and using a CSV data file is as good as any other method.

The problem is that the Postman client doesn’t have native access to the underlying file structure to save responses.

You might want to consider using Newman to run your collection in conjunction with HTMLExtra.

newman-reporter-htmlextra - npm (npmjs.com)

Or consider using another API to send your data to. For example, a SharePoint list.