Python: How to prevent requests from percent encoding in URLs?
If you need url with parameter like
key=site:dummy+type:example+group:wheel
and you use standard method
payload = {
'key': 'site:dummy+type:example+group:wheel'
}
r = requests.get(url, params=payload)
then it converts it to
key=site%3Adummy%2Btype%3Aexample%2Bgroup%3Awheel"
To get it without converting to percent you can use directly string in params
:
r = requests.get(url, params='key=site:dummy+type:example+group:wheel')
You can also automatize it if you have more parameters
payload = {
'format': 'json',
'key': 'site:dummy+type:example+group:wheel'
}
payload_str = "&".join("%s=%s" % (k,v) for k,v in payload.items())
# 'format=json&key=site:dummy+type:example+group:wheel'
url = 'https://httpbin.org/get'
r = requests.get(url, params=payload_str)
print(r.text)
You can also use urllib.parse.urlencode(...)
with param safe=
to inform what chars it shouldn't convert. In this example it should safe=':+'
import requests
import urllib.parse
payload = {
'format': 'json',
'key': 'site:dummy+type:example+group:wheel'
}
payload_str = urllib.parse.urlencode(payload, safe=':+')
# 'format=json&key=site:dummy+type:example+group:wheel'
url = 'https://httpbin.org/get'
r = requests.get(url, params=payload_str)
print(r.text)
If you like it
Buy a Coffee
Buy a Coffee