Httpx: how to post data as bytes. such as response = requests.post(url="xxx", data=bytearray(b'123'))

Created on 2 Dec 2020  路  4Comments  路  Source: encode/httpx

File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpx/_client.py", line 984, in post
return self.request(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpx/_client.py", line 721, in request
return self.send(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpx/_client.py", line 753, in send
response = self._send_handling_auth(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpx/_client.py", line 791, in _send_handling_auth
response = self._send_handling_redirects(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpx/_client.py", line 823, in _send_handling_redirects
response = self._send_single_request(request, timeout)
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpx/_client.py", line 853, in _send_single_request
(status_code, headers, stream, ext) = transport.request(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpcore/_sync/http_proxy.py", line 124, in request
return self._tunnel_request(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpcore/_sync/http_proxy.py", line 248, in _tunnel_request
(status_code, headers, stream, ext) = connection.request(
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpcore/_sync/connection.py", line 100, in request
return self.connection.request(method, url, headers, stream, ext)
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpcore/_sync/http2.py", line 119, in request
return h2_stream.request(method, url, headers, stream, ext)
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpcore/_sync/http2.py", line 294, in request
self.send_body(stream, timeout)
File "/Users/daisixuan/.pyenv/versions/3.8.2/lib/python3.8/site-packages/httpcore/_sync/http2.py", line 338, in send_body
chunk_size = min(len(data), max_flow)
TypeError: object of type 'int' has no len()
question

All 4 comments

Like so... httpx.post('https://www.httpbin.org/', data=b'123')

I We could feasibly also add bytearray support here... https://github.com/encode/httpx/blob/d0835da230892bfa7ef42a5aed9dba8122a4359e/httpx/_content.py#L77-L80

Not sure if that's really necessary or not. Normally we're guarding against type errors here, but we're checking for iterable as one of our cases, and bytearray is an iterable, so it's passing through happily until the point it raises an exception inside httpcore.

thx 馃馃馃

Also I think we now tend to suggest content=... rather than data=... for plain bytes (that was kept for compatibility but et type hints disallow it).

Well,I tried and succeeded.Thank you for your help

Was this page helpful?
0 / 5 - 0 ratings