-
Notifications
You must be signed in to change notification settings - Fork 1k
uaiohttpclient does not support POST/PUT data #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
i think i will use this lib instead: https://github.com/Carglglz/asyncmd/blob/main/async_modules/async_urequests/async_urequests.py Here is my untested attempt at a fix:
Changes:
Hopefully i got |
I did the same, then found your issue about it 🙈 Here is my attempt, only json, no header support and allow to use a port other than 80. import uasyncio as asyncio
class ClientResponse:
def __init__(self, reader):
self.content = reader
def read(self, sz=-1):
return (yield from self.content.read(sz))
def __repr__(self):
return "<ClientResponse %d %s>" % (self.status, self.headers)
class ChunkedClientResponse(ClientResponse):
def __init__(self, reader):
self.content = reader
self.chunk_size = 0
def read(self, sz=4 * 1024 * 1024):
if self.chunk_size == 0:
l = yield from self.content.readline()
# print("chunk line:", l)
l = l.split(b";", 1)[0]
self.chunk_size = int(l, 16)
# print("chunk size:", self.chunk_size)
if self.chunk_size == 0:
# End of message
sep = yield from self.content.read(2)
assert sep == b"\r\n"
return b""
data = yield from self.content.read(min(sz, self.chunk_size))
self.chunk_size -= len(data)
if self.chunk_size == 0:
sep = yield from self.content.read(2)
assert sep == b"\r\n"
return data
def __repr__(self):
return "<ChunkedClientResponse %d %s>" % (self.status, self.headers)
def request_raw(method, url, json=None):
data = None
try:
proto, dummy, host, path = url.split("/", 3)
except ValueError:
proto, dummy, host = url.split("/", 2)
path = ""
if proto == "http:":
port = 80
else:
raise ValueError("Unsupported protocol: " + proto)
if ":" in host:
host, port = host.split(":", 1)
port = int(port)
reader, writer = yield from asyncio.open_connection(host, port)
# Use protocol 1.0, because 1.1 always allows to use chunked transfer-encoding
# But explicitly set Connection: close, even though this should be default for 1.0,
# because some servers misbehave w/o it.
yield from writer.awrite(b"{} /{} HTTP/1.0\r\nHost: {}\r\nUser-Agent: compat\r\n".format(method, path, host))
if json is not None:
import ujson
data = ujson.dumps(json)
yield from writer.awrite(b"Content-Type: application/json\r\n")
yield from writer.awrite(b"Content-Length: {}\r\n".format(len(data)))
yield from writer.awrite(b"Connection: close\r\n\r\n")
if data:
yield from writer.awrite(data)
# yield from writer.aclose()
return reader
def request(method, url, json=None):
redir_cnt = 0
redir_url = None
while redir_cnt < 2:
reader = yield from request_raw(method, url, json)
headers = []
sline = yield from reader.readline()
sline = sline.split(None, 2)
status = int(sline[1])
chunked = False
while True:
line = yield from reader.readline()
if not line or line == b"\r\n":
break
headers.append(line)
if line.startswith(b"Transfer-Encoding:"):
if b"chunked" in line:
chunked = True
elif line.startswith(b"Location:"):
url = line.rstrip().split(None, 1)[1].decode("latin-1")
if 301 <= status <= 303:
redir_cnt += 1
yield from reader.aclose()
continue
break
if chunked:
resp = ChunkedClientResponse(reader)
else:
resp = ClientResponse(reader)
resp.status = status
resp.headers = headers
return resp Works for me since a few days without problems |
So I believe this issue can be closed. |
Usage:
await uaiohttpclient.request(method,url)
micropython-lib/micropython/uaiohttpclient/uaiohttpclient.py
Line 65 in 7128d42
Function has no way to take data or set the header content type for that matter
The text was updated successfully, but these errors were encountered: