0

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.

2
  • 1
    Well right now ID is a string, and iterating over a string just returns the individual characters. Either do ID = ['first_id', 'second_id', ...] or do for x in ID.split(',') Commented Mar 25, 2021 at 16:15
  • @Lagerbaer using ID = ['first_id', 'second_id'] doesn't work since it is showing TypeError: can only concatenate str (not "list") to str. Using for x in ID.split(',') is only pulling back the last ID that was passed Commented Mar 25, 2021 at 23:32

1 Answer 1

1

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)  
Sign up to request clarification or add additional context in comments.

4 Comments

shouldn't it be the ID field that needs to be split? What is the purpose of splitting the fields list?
what is the difference really. All you need to do is change the values accordingly, and both of the strings have the exact same format.
the point is that you use the .split() string method
The above works, with one adjustment - the dict. output from the response has to be appended into a single dict since the for loop overwrites the previous value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.