0

If I have %20 in URL before, it ends up with + in the code.

url='http://www.example.com?param=a%20b'
url=URL(url)
url=url.copy_merge_params(params={})

In QueryParams.str it's invoking urlencode() without any additional arguments. Is there a way to stop this behaviour short of manipulating URLs only without using any params for AsyncClient.request()?

aikipooh
  • 137
  • 1
  • 19

1 Answers1

0

in my knowledge, i think you can override from the URL behavior class or you can also do it manually by encoding the query parameters and replacing the "+" characters with the value of "%20" to retain the original URL. afterwards, you can make a request by calling AsyncClient.request()

import httpx
from urllib.parse import urlencode, urlparse, urlunparse

# you can also use parameters to preserve other params value
# just in case if you might need to update with a value other than "%20"
def custom_encode_params(params, new_value):
    encoded_params = urlencode(params)
    return encoded_params.replace("+", new_value)

base_url = "http://example.com"
params = {"param": "a b"}

encoded_params = custom_encode_params(params, "%20")
parsed_url = urlparse(base_url)
new_url = urlunparse(parsed_url._replace(query=encoded_params))

# adjust with your code that uses `AsyncClient.request()` method
client = httpx.Client()
response = client.get(new_url)
print(response.url)

the URL appending process is indeed done manually and this possibility doesnt cover some other edge cases according to your needs. Hope this helps

  • Thank you for answering! I don't remember what it was, probably a note for Andrew Svetlov or some other httpx developer:) – aikipooh Aug 31 '23 at 10:26