Seba Chamena
Seba Chamena

Reputation: 35

Saving multiple requests in a single JSON

My problem is the following: I've a list of Ips that I sorted in an nparray (ip_array), then I wanna do a multiple request with all of them and saving the outputs in a single json. (the APIKEY is really the api key in the code xD)

url_auth = 'https://api.ipgeolocation.io/ipgeo?apiKey=APIKEYAPIKEYAPIKEY='
for i in np.arange(1,4): 
    r[i] = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]}) #i tested the single request and it works in this way.

But then, i got

TypeError: 'Response' object does not support item assignment

And then, i tried replacing the last line with

 r = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]})

But, when i do

r.json()

I only get the last request (that is obvious).

Upvotes: 2

Views: 1191

Answers (1)

Devstr
Devstr

Reputation: 4641

Store response on every iteration:

url_auth = 'https://api.ipgeolocation.io/ipgeo?apiKey=APIKEYAPIKEYAPIKEY='
responses = []
for i in np.arange(1,4): 
    response = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]})
    responses.append(response.json())

responses list will contain all response objects.

Upvotes: 3

Related Questions