How to set up API get query parameters in python request

Question:

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?

Asked By: dhc

||

Answers:

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)
Answered By: Silvio Mayolo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.