bug: strings not accepted in files param #1897
Answered
by
lovelydinosaur
adriangb
asked this question in
Potential Issue
import requests
import httpx
# requests works
resp = requests.post("https://httpbin.org/post", files=[("name", "content")])
assert resp.status_code == 200
assert resp.json()["files"] == {"name": "content"}
# httpx fails, despite the signature of `files` saying it accepts a 2 tuple of (name: str, data: Union[str, bytes, file])
httpx.post("https://httpbin.org/post", files=[("name", "content")]) # raises a TypeError
assert resp.status_code == 200
assert resp.json()["files"] == {"name": "content"} |
Answered by
lovelydinosaur
Oct 15, 2021
Replies: 1 comment 1 reply
|
So... FileContent = Union[IO[bytes], bytes]
FileTypes = Union[
# file (or text)
FileContent,
# (filename, file (or text))
Tuple[Optional[str], FileContent],
# (filename, file (or text), content_type)
Tuple[Optional[str], FileContent, Optional[str]],
]
RequestFiles = Union[Mapping[str, FileTypes], Sequence[Tuple[str, FileTypes]]]Those comments look like they need updating tho'. They're not in line with what We switched from accepting text-or-bytes to bytes-only here. (We need to be able to know upfront how many bytes the content will be, and files opened in text mode don't allow us to do that, but text strings could be allowable, perhaps.) |
1 reply
Answer selected by
adriangb
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So...
contentthere needs to be bytes - the annotation is derived from here... https://github.com/encode/httpx/blob/master/httpx/_types.py#L84-L93Those comments look like they need updating tho'. They're not in line with what
FileContentactually accepts.We switched from accepting text-or-bytes to bytes-only here.
We could feasibly reconsider t…