Skip to content

Instantly share code, notes, and snippets.

@mjbear
Created November 3, 2023 00:07
Show Gist options
  • Save mjbear/0e30761e2f8c4551aafd857a71706ba5 to your computer and use it in GitHub Desktop.
Save mjbear/0e30761e2f8c4551aafd857a71706ba5 to your computer and use it in GitHub Desktop.
override urllib3 allowed chars so requests will accept otherwise invalid characters
import requests
import urllib3.util.url
# ref: Lyle at https://github.com/psf/requests/issues/6115#issuecomment-1172198984
def hook_invalid_chars(component, allowed_chars):
# handle url encode here, or do nothing
return component
# Method1:
# Warning: this is a hack to override
# urllib3.util.url._encode_invalid_chars = hook_invalid_chars
# Method2:
# ref: Tomi at https://stackoverflow.com/a/77256282/22707650
# Warning: this is a hack to make urllib3 allow otherwise invalid characters
urllib3.util.url._QUERY_CHARS.add('{')
urllib3.util.url._QUERY_CHARS.add('}')
url_base = 'http://127.0.0.1:5470/thing/page'
url_query = '{item1}{item2}{item3}'
with requests.Session() as sess:
# Note: need to use both the hack above and a prepared request
req = requests.Request(method='GET', url=url_base)
prep = req.prepare()
prep.url += f'?{url_query}'
resp = sess.send(prep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment