Skip to content

Commit

Permalink
fix encoding data with None values and more
Browse files Browse the repository at this point in the history
  • Loading branch information
karosc committed May 3, 2024
1 parent 1148713 commit 1380fb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/niquests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,12 @@ def _encode_params(
result = []
for k, vs in to_key_val_list(data):
iterable_vs: typing.Iterable[str | bytes]
if isinstance(vs, (str, bytes, int, float, bool)):
# not officially supported, but some people maybe passing ints, float or bool.
if isinstance(vs, (str, bytes)) is False:
if isinstance(vs, (str, bytes, int, float, bool)) or not hasattr(
vs, "__iter__"
):
# not officially supported, but some people maybe passing ints, float, bool,
# or other string serializable classes.
if not isinstance(vs, (str, bytes)):
iterable_vs = [str(vs)]
else:
iterable_vs = [vs]
Expand Down
22 changes: 22 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,28 @@ def test_json_encodes_as_bytes():
assert isinstance(p.body, bytes)


def test_data_ecodes_noniterables():
class Class:
pass

class ClassStringable:
def __str__(self):
return "🔥arbClassStringable🔥"

body = {
"string": "string",
"float": 0.01,
"int": 100,
"bool": True,
"None": None,
"plain_class": Class(),
"stringable_class": ClassStringable(),
}
p = PreparedRequest()
p.prepare(method="post", url="https://www.example.com/", data=body)
assert isinstance(p.body, str)


def test_requests_are_updated_each_time(httpbin):
session = RedirectSession([303, 307])
prep = niquests.Request("POST", httpbin("post")).prepare()
Expand Down

0 comments on commit 1380fb6

Please sign in to comment.