encode / httpx

A next generation HTTP client for Python. 🦋
https://www.python-httpx.org/
BSD 3-Clause "New" or "Revised" License
12.7k stars 811 forks source link

Httpx is not encoding "%" symbol while sending requests #3140

Closed heysarthak closed 2 months ago

heysarthak commented 3 months ago

Discussed in https://github.com/encode/httpx/discussions/3094

Originally posted by **heysarthak** February 14, 2024 Hi team, I am ussing httpx plugin, while sending httpx query "ngi%abc.com" . ideally "%" symbol should be encoded as "%25" but it is still sending as the same "%". can we please have a fix for this.
deedy5 commented 3 months ago

% character has a special meaning in URLs, representing the start of a percent-encoded character. To include a literal % character in a URL, it must be percent-encoded as %25.

You can manually encode the query parameters in Python using the urllib.parse module:


from urllib.parse import quote
import httpx

query_param = "ngi%abc.com"
encoded_query_param = quote(query_param)

url = f"https://example.com/search?q={encoded_query_param}"
resp = httpx.get(url)