How to prevent python requests from percent encoding my URLs?

It is not good solution but you can use directly string:

r = requests.get(url, params="format=json&key=site:dummy+type:example+group:wheel")

BTW:

Code which convert payload to this string

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'

r = requests.get(url, params=payload_str)

EDIT (2020):

You can also use urllib.parse.urlencode(...) with parameter safe=":+" to create string without converting chars :+ .

As I know requests also use urllib.parse.urlencode(...) for this but without 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)

I used page https://httpbin.org/get to test it.

Leave a Comment