I thought this was simple., but I already spend some time without any success. I have a client that sends data via a JSON POST request. I want to receive this data from my ESP32 running Picoweb and later display the content on the attached screen.
I got Picoweb up and running and all GET requests are working fine.
I replicated the client's request by a simple curl call:
Code: Select all
curl -H "Content-Type: application/json" -H "Accept: application/json" -H "Content-Length: 155" -X POST -d '{"pressure": 997, "voltage": 12.184, "mintemp": 27.7, "humidity": 26, "temperature": 29.4, "ts": 1591796818, "trend": 0, "maxtemp": 30.0, "hallsensor": 46}' http://192.168.42.192/batteryslave
Code: Select all
def index(req, resp):
method = req.method
print("Method was:" + method)
yield from picoweb.start_response(resp)
yield from resp.awrite("Hello world from picoweb running on the ESP32")
def batteryslave(req, resp):
method = req.method
print("Method was:" + method)
if req.method == "POST":
size = int(req.headers[b"Content-Length"])
print("Size: ", size)
yield from req.read_form_data()
else:
req.parse_qs()
yield from picoweb.start_response(resp)
yield from resp.awrite("OK")
yield from resp.awrite("\r\n")
print(req.headers)
ROUTES = [
("/", index),
("/batteryslave", batteryslave)
]
app = picoweb.WebApp(__name__, ROUTES)
app.run(debug=2, port = 80, host = "192.168.42.192")
I instantly get a "Connection reset by peer" and picweb is throwing an error: "AttributeError: 'Stream' object has no attribute 'readexactly'" which is caused by the req.read_form_data().
So now I put a comment in front of the "read_form_data" I go get all my debug information:
Code: Select all
INFO:picoweb:645119762.421 <HTTPRequest object at 3ffe5fd0> <Stream object at 3ffe5dd0> "POST /batteryslave"
Method was:POST
Size: 155
{b'Accept': b'application/json', b'User-Agent': b'curl/7.64.0', b'Host': b'192.168.42.192', b'Content-Type': b'application/json', b'Content-Length': b'155'}
DEBUG:picoweb:645119762.421 <HTTPRequest object at 3ffe5fd0> Finished processing request
So how do I read the raw data, so that I can decode the json data?
And how do I send a reply back to the client, so that he know everything was successful.
Thanks...