I use the code below in python on a rpi to do json posts
import requests
payload = '{"msg_type":"observation_payload","msg":
{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload
test","t":"1543540780"}}}}'
r = requests.post('https://my.website/api/observations', data =
{'post_wrapper':payload})
print r.text
it produces
{"msg": {"remote_pi": "FZtank", "stored_ids": ["8"]}, "msg_type": "receipt"}
I figured it wouldn't be too hard to do the same thing in micropython on a pycom device
import urequests as requests
payload = '{"msg_type":"observation_payload","msg":
{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload
test","t":"1543540780"}}}}'
r = requests.post('https://my.website/api/observations', data =
{"post_wrapper":payload})
print(r.text)
r.close()
but it gives
Traceback (most recent call last):
File "<stdin>", line 12, in <module>
File "/flash/lib/urequests.py", line 115, in post
File "/flash/lib/urequests.py", line 100, in request
File "/flash/lib/urequests.py", line 79, in request
TypeError: object with buffer protocol required
The TypeError msg means it wants a string instead of a dictionary for the data
so I tried
import urequests as requests
payload = '{"post_wrapper":{"msg_type":"observation_payload","msg":
{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload
test","t":"1543540780"}}}}}'
r = requests.post('https://my.website/api/observations', data =payload)
print(r.text)
r.close()
but that gives
{"msg": {"errorText": "Malformed POST - unable to decode JSON"}, "msg_type":
"error"}
So now I'm confused. Is the problem that the website expects a dictionary & micropython can only send a string?
.
Some further detail: I'm using https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py but with line 52 changed from ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) to ai = usocket.getaddrinfo(host, port) because it used to throw an error msg about 4 arguments Vs 2. If I run
import urequests as requests
payload = '{"post_wrapper":{"msg_type":"observation_payload","msg":
{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload
test","t":"1543540780"}}}}}'
r = requests.post('https://my.website/api/observations', data=payload)
or
import urequests as requests
payload = {"post_wrapper":{"msg_type":"observation_payload","msg":
{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload
test","t":"1543540780"}}}}}
r = requests.post('https://my.website/api/observations', json=payload)
or
import urequests as requests
payload = {"msg_type":"observation_payload","msg":
{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload
test","t":"1543540780"}}}}
r = requests.post('https://my.website/api/observations', json=
{"post_wrapper":payload})
I get the same result
{"msg": {"errorText": "Malformed POST - unable to decode JSON"}, "msg_type":
"error"}
json={'post_wrapper': payload}though?urlcan effect the port; are you sure that's not a seperate unrelated issue? Also maybe post full error messages including line numbers, that's much easier to get help for.