From d5900cd40ec85e47800e21e77bc684b9f65325ed Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 2 Sep 2022 14:24:45 +0100 Subject: [PATCH] Fix empty query params (#2354) Co-authored-by: Marcelo Trylesinski --- httpx/_urls.py | 2 +- tests/models/test_queryparams.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/httpx/_urls.py b/httpx/_urls.py index cf4df384c1..1211bbba9a 100644 --- a/httpx/_urls.py +++ b/httpx/_urls.py @@ -540,7 +540,7 @@ def __init__( items: typing.Sequence[typing.Tuple[str, PrimitiveData]] if value is None or isinstance(value, (str, bytes)): value = value.decode("ascii") if isinstance(value, bytes) else value - self._dict = parse_qs(value) + self._dict = parse_qs(value, keep_blank_values=True) elif isinstance(value, QueryParams): self._dict = {k: list(v) for k, v in value._dict.items()} else: diff --git a/tests/models/test_queryparams.py b/tests/models/test_queryparams.py index ba200f146d..29b2ca634d 100644 --- a/tests/models/test_queryparams.py +++ b/tests/models/test_queryparams.py @@ -76,6 +76,17 @@ def test_queryparam_types(): assert str(q) == "a=1&a=2" +def test_empty_query_params(): + q = httpx.QueryParams({"a": ""}) + assert str(q) == "a=" + + q = httpx.QueryParams("a=") + assert str(q) == "a=" + + q = httpx.QueryParams("a") + assert str(q) == "a=" + + def test_queryparam_update_is_hard_deprecated(): q = httpx.QueryParams("a=123") with pytest.raises(RuntimeError):