httpx
httpx copied to clipboard
Stricter data typechecking
trafficstars
Closes #3127
Before...
>>> import httpx
>>> data = {"key": {"this": "ain't json, buddy"}}
>>> r = httpx.post("https://www.example.com", data=data)
>>> r.request.content
b'key=%7B%27this%27%3A+%22ain%27t+json%2C+buddy%22%7D'
After...
>>> import httpx
>>> data = {"key": {"this": "ain't json, buddy"}}
>>> r = httpx.post("https://www.example.com", data=data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_api.py", line 331, in post
return request(
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_api.py", line 118, in request
return client.request(
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_client.py", line 825, in request
request = self.build_request(
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_client.py", line 359, in build_request
return Request(
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_models.py", line 342, in __init__
headers, stream = encode_request(
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_content.py", line 235, in encode_request
return encode_urlencoded_data(data)
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_content.py", line 167, in encode_urlencoded_data
plain_data.append(_coerce_type(key, value))
File "/Users/tom.christie/GitHub/encode/httpx/httpx/_content.py", line 153, in _coerce_type
raise TypeError(
TypeError: Request data values must be str, int, float, bool, or None. Got type 'dict' for key 'key'.
I've not changed any type signatures here, and aimed to keep the PR as focused as possible.
Ideally we should be following up with more comprehensive documentation on the behaviour of content=..., data=..., files=..., json=....
Oh... thanks for the fast turnaround @zanieb. 😊