Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set up API get query parameters in python request

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading