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).
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?
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!
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)`