Skip to content
Myr

Use '%20' for encoding spaces in query parameters. (#2543)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add failing test * Fix failing test case * Add urlencode * Update comment

authored by Tom Christie · committed by GitHub · parent 57daabf · Browse files · 3 files+41 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+41 added−2 removed

  • tests:+16 added−0 removed
  • other:+25 added−2 removed

3 files+41 added−2 removed

modified:httpx/_urlparse.py+16 added
431 unmodified lines
432433 return "".join(434 [char if char in NON_ESCAPED_CHARS else percent_encode(char) for char in string]435 )436437438def urlencode(items: typing.List[typing.Tuple[str, str]]) -> str:439 # We can use a much simpler version of the stdlib urlencode here because440 # we don't need to handle a bunch of different typing cases, such as bytes vs str.441 #442 # https://github.com/python/cpython/blob/b2f7b2ef0b5421e01efb8c7bee2ef95d3bab77eb/Lib/urllib/parse.py#L926443 #444 # Note that we use '%20' encoding for spaces, and treat '/' as a safe445 # character. This means our query params have the same escaping as other446 # characters in the URL path. This is slightly different to `requests`,447 # but is the behaviour that browsers use.448 #449 # See https://github.com/encode/httpx/issues/2536 and450 # https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode451 return "&".join([quote(k) + "=" + quote(v) for k, v in items])
modified:httpx/_urls.py+9 added-2 removed
1import typing2from urllib.parse import parse_qs, unquote, urlencode2from urllib.parse import parse_qs, unquote34import idna56from ._types import PrimitiveData, QueryParamTypes, RawURL, URLTypes7from ._urlparse import urlparse7from ._urlparse import urlencode, urlparse8from ._utils import primitive_value_to_str91011class URL:
603 unmodified lines
615 return False616 return sorted(self.multi_items()) == sorted(other.multi_items())617618 def __str__(self) -> str:619 """620 Note that we use '%20' encoding for spaces, and treat '/' as a safe621 character.622623 See https://github.com/encode/httpx/issues/2536 and624 https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode625 """626 return urlencode(self.multi_items())627628 def __repr__(self) -> str:629 class_name = self.__class__.__name__
14 unmodified lines
modified:tests/models/test_url.py+16 added
348 unmodified lines
349 assert url.query == b""350 assert url.raw_path == b"/path?"351352353def test_url_query_encoding():354 """355 URL query parameters should use '%20' to encoding spaces,356 and should treat '/' as a safe character. This behaviour differs357 across clients, but we're matching browser behaviour here.358359 See https://github.com/encode/httpx/issues/2536360 and https://github.com/encode/httpx/discussions/2460361 """362 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")363 assert url.raw_path == b"/?a=b%20c&d=e/f"364365 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})366 assert url.raw_path == b"/?a=b%20c&d=e/f"367368369def test_url_with_url_encoded_path():370 url = httpx.URL("https://www.example.com/path%20to%20somewhere")371 assert url.path == "/path to somewhere"372 assert url.query == b""
52 unmodified lines