httpx icon indicating copy to clipboard operation
httpx copied to clipboard

Stricter type checking on request data.

Open lovelydinosaur opened this issue 1 year ago • 1 comments
trafficstars

  • [x] Initially raised as discussion #3118

Here's a gnarly thing...

data = {"key": {"this": "ain't json, buddy"}}
httpx.post("https://www.example.com", data=data)

Current behavior is that we'll coerce the value to a string, because we're overly lenient on the types...

>>> 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'

Ideally we ought to raise a type error instead...

TypeError: Request data values must be str, int, float, bool, or None. Got type 'dict' for key 'key'.

(For comparison requests also leniently allows this, and also ends up sending unexpected data.)

lovelydinosaur avatar Mar 01 '24 15:03 lovelydinosaur

👆🏼 Hmmm... still experimenting with polar here. fund.

lovelydinosaur avatar Mar 01 '24 15:03 lovelydinosaur

See also https://github.com/encode/httpx/issues/3176#issuecomment-2313220498

lovelydinosaur avatar Aug 27 '24 18:08 lovelydinosaur