pycoin
pycoin copied to clipboard
Incorrect POST data encoding from blockcypher.py
File "/home/charlieb/src/pycoin/pycoin/services/blockcypher.py", line 75, in broadcast_tx result = json.loads(urlopen(url, data=json.dumps(data)).read().decode("utf8")) File "/home/charlieb/src/pycoin/pycoin/services/agent.py", line 17, in urlopen return request.urlopen(req) File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.6/urllib/request.py", line 524, in open req = meth(req) File "/usr/lib/python3.6/urllib/request.py", line 1248, in do_request_ raise TypeError(msg) TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
I added at blockcypher.py:73
print(json.dumps(data).encode('utf8'))
print(json.dumps(data))
As you can see from the following the encode results in a bytes iterable but just a dump results in a python dict which urlopen must be converting to a string.
b'{"tx": "<identical txhash>"}'
{"tx": "<identical txhash>"}
The python3 documentation here agrees with the error message
https://docs.python.org/3/library/urllib.request.html#urllib.request.Request
For an HTTP POST request method, data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns an ASCII string in this format. It should be encoded to bytes before being used as the data parameter.