How to set up API get query parameters in python request

Advertisements

I’m trying to access an API endpoint in python. Generically, the request needs to look like this. In my real world example, (replacing URL with the actual endpoint), this works as expected. Notice the metanames arg is called multiple times

res = requests.get('url?someargs&metanames=part1&metanames=part2')

However, I’m now trying to do it this way:

params = {'metanames':'part1', 'metanames':'part2'}
url = "http:url"
res = requests.get(url = url, params = params)

This is pseudocode, but nontheless the metanames arg is not printed multiple times as I want it to be as in the example up top.

Can anyone advise on the right way to set that up in the dictionary so it mirros example 1?

>Solution :

Python dictionaries don’t support duplicate keys. But if you have a list as a parameter value in your params dictionary, the Requests library will translate that into multiple URL parameters.

params = {'metanames': ['part1', 'part2']}
url = "http:url"
res = requests.get(url = url, params = params)

Leave a ReplyCancel reply