Using Response to format URL for next Query

I am very new to working with APIs and I am not at all a programmer. That said, I am trying!

What I need to do is format a URL from my previous querys response, and its working, but I have formatting issues.

Query A is returning a list of ids like this:

“resources”: [
“8cdf5f894f59da04e15”,
“fa79f22a729d239f288a3be0”,
“74d6301c15314ec2da473”,
“e33e5bb3fba859e8e”,
“01814d238c01832d5fa11”,
“113b8890”,
“98cc69e304cc4139f760”,

],
“errors”:
}

What I need to do is pass the resources to the URL for my next query like:

https://api.example.com/query/v1?ids=8cdf5f894f59da04e15&ids=fa79f22a729d239f288a3be0&ids=74d6301c15314ec2da473… and so on.

What is happening is this:

https://api.example.com/query/v1?ids=8cdf5f894f59da04e15,fa79f22a729d239f288a3be0&,74d6301c15314ec2da473.

Can anyone point me in the right direction here? I am lost!

Thanks

Hey @sKtwsU6mjAuNcck! Welcome to the Postman community :tada:

I understand you would like to pass each element in the response array from the previous request separately to a query parameter, correct?

You can pass the values to the next request using environment variable (place the following code in the Test tab of the previous request):

const resources = pm.response.json().resources
let queryParamString = ''
resources.forEach((id, index, array) => {
    queryParamString += index!==array.length-1 ? `ids=${id}&` : `ids=${id}`
})
pm.environment.set('queryParamString', queryParamString)

And then you can replace the next request with following:
https://api.example.com/query/v1?{{queryParamString}}

I hope this helps!
Please let me know if you have further questions :smiley:

This works perfectly. What I ultimately need is for this to be a python script and I dont know much about Python. I tried going to code and selecting python, but it doesnt seem to work and the Test script doesnt appear in the Python code. Any idea on how someone with 0 knowledge of Python can turn these into a functional python script?

Hey @sKtwsU6mjAuNcck,

Thanks for getting back!
I am glad that the script works for you :smiley:

In Python, it would be as follows (you will need to replace the array values):

resources = ["a", "b", "c"]
string = ""
for i, id in enumerate(resources):
    if i != len(resources)-1:
        string+= id + "&"
    else:
        string+= id

print(string)

Hope this helps!
Please let me know if you have further questions :wink: