Reputation: 8025
I am using python's requests library to do a 'GET' from an API. here is part of my code :
payload = { 'topicIds':'128487',
'topicIds':'128485',
'topicIds': '242793',
'timePeriod':'10d', }
r= requests.get(url, params=payload, headers=headers)
According to the API documentation, we can assign multiple topicIds to one request like this: <url>topicId=123&topicId=246
when i try to set topicIds value as a list like this:
payload = { 'topicIds':['128487' , '242793'],
I get an error : {u'error': u'topicIds: has 2 terms, should be between 0 and 1'}
However when i run the code, i only get data from the last topicIds => 'topicIds': '242793' Am i writing the payload dictionary wrongly?
Thanks,
Upvotes: 9
Views: 12783
Reputation: 3455
This would work as well
params = {'topicIds': ['128487', '128485', '242793'],
'timePeriod':'10d', }
r= requests.get(url, params=params)
Upvotes: 3
Reputation: 2576
Try:
payload = {'topicIds[]': ['128487', '242793']}
r = requests.get(url, params=payload, headers=headers)
This is the most common way of defining arrays in query strings.
Upvotes: 12
Reputation: 121
Yes you are writing payload wrongly, just try to print this dict, you will get
{'topicIds': '242793', 'timePeriod': '10d'}
The keys are overridden in dictionary
I think that it would be better, to make one string out of all topicIds, something like that
'<separator>'.join([<list of topicIds>])
Upvotes: -2