import requests
import json
url = "https://node1.web3api.com/"
payload = json.dumps({
"jsonrpc": "2.0",
"id": 2,
"method": "eth_call",
"params": [
{
"from": "0x0000000000000000000000000000000000000000",
"data": "0xc87b56dd00000000000000000000000000000000000000000000000000000000000004d2",
"to": "0x792496a3f678187e59e1d1d5e075799cd1e124c2"
},
"latest"
]
})
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://etherscan.io/',
'Content-Type': 'application/json',
'Origin': 'https://etherscan.io',
'Connection': 'keep-alive',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'cross-site',
'TE': 'trailers'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
print语句将打印以下内容:K4f+的二次谐波
我试着这样做:
import requests
import json
url = "https://node1.web3api.com/"
payload = json.dumps({
"jsonrpc": "2.0",
"id": 2,
"method": "eth_call",
"params": [
{
"from": "0x0000000000000000000000000000000000000000",
"data": "0xc87b56dd00000000000000000000000000000000000000000000000000000000000004d2",
"to": "0x792496a3f678187e59e1d1d5e075799cd1e124c2"
},
"latest"
]
})
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://etherscan.io/',
'Content-Type': 'application/json',
'Origin': 'https://etherscan.io',
'Connection': 'keep-alive',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'cross-site',
'TE': 'trailers'
}
response = requests.request("POST", url, headers=headers, data=payload)
print("ENCODING: ", response.encoding)
print(response.json())
第二个实现返回以下错误:
ENCODING: utf-8
requests.exceptions.JSONDecodeError: [Errno Expecting value] ��D��R���Ӥ����?l��`�I ��h��'���x=Ϥ�d3��rϚ�^��@�S�D���Ė��s��"�TZL�yeyD�gfT"*���H��'(GD��k,`�XQ��fK4f+�: 0
最终,我应该会收到以下响应:
{"jsonrpc":"2.0","id":2,"result":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003a697066733a2f2f516d564c62664470426a3958785843436757776873687041514539583233736b5a3853667055506e323948686e512f31323334000000000000"}
我知道这是我应该得到的响应,因为这是我在浏览器和Postman中发出请求时得到的响应。我只是试图通过python发出相同的HTTP请求。
我应该怎么做才能解码响应?
1条答案
按热度按时间smdncfj31#
您需要对输出进行解码。
1.正在初始化web3(遵循here的官方以太坊文档,
1.接下来,我们需要指定解码后的JSON是什么样的,我们必须提到数据类型(of Solidity)和参数的名称。
1.使用已解码的字符串调用decodeParameters()函数
res
将属于以下类型:**其他:**如果您预期的JSON包含嵌套元素,则需要使用
components
,这是您声明typesArray
的方式。