user10
user10

Reputation: 167

loop to pass bulk values into python api request

I have an API built in Python that outputs data and takes in the input parameter 'ID', and outputs a set of fields. The parameter does not allow bulk values to be passed and to work around this, I have tried to create a loop to make one call per Id. Below is an example of what I tried:

    ID = '19727795,19485344'
#15341668,
fields = 'userID, agentID, internalID'
 
#add all necessary headers
header_param = {'Authorization': 'bearer ' + accessToken,'content-Type': 'application/x-www-form-urlencoded','Accept': 'application/json, text/javascript, */*'}

for x in ID:
    response = requests.get(Baseuri + '/services/v17.0/agents/' + x + '?fields=' + fields , headers = header_param) 
 

Even this loop returns an error '404 Invalid ID'

What is the way to pass a list of args into the ID parameter? I have to run this code at least once a day and need a way to pass multiple values in.

Upvotes: 0

Views: 596

Answers (1)

Noah
Noah

Reputation: 194

If it does not allow bulk IDS and it is your API, you have two choices that I see. You either A) allow it server-side, or B) do this:

fields = 'userID, agentID, internalID'
field_list = fields.split(",")

for field in field_list:
    pass

Really, it just boils down to if you want your client-side code to be more simple or your server-side to be more simple because it is more or less the same process regardless of the end it is on. On the other hand, f-strings are more efficient and cleaner (subjective) to use:

response = requests.get(Baseuri + '/services/v17.0/agents/' + x + '?fields=' + fields , headers = header_param)

will turn into:

response = requests.get(f'{BASE_URI}/services/v17.0/agents/{x}?fields={fields}', headers = header_param)  

Upvotes: 1

Related Questions