I know this sounds weird at first glance. Let me explain.
I have a domain that stays consistent, say https://some.api.endpoint/some/path
I have a list of different queries I need to call with it. One way to do it is simple concatenating strings but then I have to deal with encoding, something I try to avoid.
So I decided to go with using urlparse -> urlunparse. I'm not sure if this is a good practice, but this can work.
url = 'https://some.api.endpoint/some/path'
parsed = urlparse(url)
parsed=parsed._replace(query='search=some_name')
new_url = urlunparse(parsed)
response = requests.get(new_url)
My questions:
urlunparse
part? From the requests
documentation, you can pass a dictionary to the params
keyword argument of requests.get
. http://docs.python-requests.org/en/master/user/quickstart/
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)
That'll handle the url encoding and everything for you.
Result from the above snippet:
print(r.url)
# http://httpbin.org/get?key2=value2&key1=value1