jxn jxn - 23 days ago 6
HTTP Question

HTTP requests assign multiple values to a key in params

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,

Answer

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.