[Ethereum] “code”:-32600,”message”:”Could not decode request”

go-ethereumjson-rpc

I am trying to send requests to the jsonrpc server as instructed in this link https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblocktransactioncountbynumber

The command works fine in the terminal

curl http://localhost:8545 -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0xe8"],"id":1}'

However, when I execute it from Python as in the following script

import urllib
import urllib2
url = 'http://localhost:8545/'    
values = dict(jsonrpc = "2.0",
            method = "eth_getBlockTransactionCountByNumber",
            params = ["0xe8"],
            id =67)

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
req.add_header("Content-Type",'application/json')
response = urllib2.urlopen(req)
print response.read()

It returns this error

{"id":-1,"jsonrpc":"2.0","error":{"code":-32600,"message":"Could not
decode request"}}

The same error shows up when I tried

r = requests.post(url, data=str(values), allow_redirects=True)
print r.content

or

print subprocess.check_output(['curl', url, "-X",  "POST", "--data",  str(values)])

is there something wrong in my Python script?

Best Answer

I believe that your values should be encoded to json before passing to post data.

Related Topic