Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 693
jxn Avatar asked Jun 26 '14 18:06

jxn


2 Answers

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.

like image 141
scandinavian_ Avatar answered Oct 22 '22 05:10

scandinavian_


This would work as well

params = {'topicIds': ['128487', '128485', '242793'],
         'timePeriod':'10d', }

r= requests.get(url, params=params)
like image 25
Bruno Vermeulen Avatar answered Oct 22 '22 05:10

Bruno Vermeulen