postman requests.text返回编码符号

lsmepo6l  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(207)
  1. import requests
  2. import json
  3. url = "https://node1.web3api.com/"
  4. payload = json.dumps({
  5. "jsonrpc": "2.0",
  6. "id": 2,
  7. "method": "eth_call",
  8. "params": [
  9. {
  10. "from": "0x0000000000000000000000000000000000000000",
  11. "data": "0xc87b56dd00000000000000000000000000000000000000000000000000000000000004d2",
  12. "to": "0x792496a3f678187e59e1d1d5e075799cd1e124c2"
  13. },
  14. "latest"
  15. ]
  16. })
  17. headers = {
  18. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0',
  19. 'Accept': '*/*',
  20. 'Accept-Language': 'en-US,en;q=0.5',
  21. 'Accept-Encoding': 'gzip, deflate, br',
  22. 'Referer': 'https://etherscan.io/',
  23. 'Content-Type': 'application/json',
  24. 'Origin': 'https://etherscan.io',
  25. 'Connection': 'keep-alive',
  26. 'Sec-Fetch-Dest': 'empty',
  27. 'Sec-Fetch-Mode': 'cors',
  28. 'Sec-Fetch-Site': 'cross-site',
  29. 'TE': 'trailers'
  30. }
  31. response = requests.request("POST", url, headers=headers, data=payload)
  32. print(response.text)

print语句将打印以下内容:K4f+的二次谐波
我试着这样做:

  1. import requests
  2. import json
  3. url = "https://node1.web3api.com/"
  4. payload = json.dumps({
  5. "jsonrpc": "2.0",
  6. "id": 2,
  7. "method": "eth_call",
  8. "params": [
  9. {
  10. "from": "0x0000000000000000000000000000000000000000",
  11. "data": "0xc87b56dd00000000000000000000000000000000000000000000000000000000000004d2",
  12. "to": "0x792496a3f678187e59e1d1d5e075799cd1e124c2"
  13. },
  14. "latest"
  15. ]
  16. })
  17. headers = {
  18. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0',
  19. 'Accept': '*/*',
  20. 'Accept-Language': 'en-US,en;q=0.5',
  21. 'Accept-Encoding': 'gzip, deflate, br',
  22. 'Referer': 'https://etherscan.io/',
  23. 'Content-Type': 'application/json',
  24. 'Origin': 'https://etherscan.io',
  25. 'Connection': 'keep-alive',
  26. 'Sec-Fetch-Dest': 'empty',
  27. 'Sec-Fetch-Mode': 'cors',
  28. 'Sec-Fetch-Site': 'cross-site',
  29. 'TE': 'trailers'
  30. }
  31. response = requests.request("POST", url, headers=headers, data=payload)
  32. print("ENCODING: ", response.encoding)
  33. print(response.json())

第二个实现返回以下错误:

  1. ENCODING: utf-8
  2. 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

最终,我应该会收到以下响应:

  1. {"jsonrpc":"2.0","id":2,"result":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003a697066733a2f2f516d564c62664470426a3958785843436757776873687041514539583233736b5a3853667055506e323948686e512f31323334000000000000"}

我知道这是我应该得到的响应,因为这是我在浏览器和Postman中发出请求时得到的响应。我只是试图通过python发出相同的HTTP请求。
我应该怎么做才能解码响应?

smdncfj3

smdncfj31#

您需要对输出进行解码。
1.正在初始化web3(遵循here的官方以太坊文档,

  1. const Web3 = require("web3");
  2. const web3 = new Web3("https://cloudflare-eth.com");

1.接下来,我们需要指定解码后的JSON是什么样的,我们必须提到数据类型(of Solidity)和参数的名称。

  1. // Example, if JSON is like this, then typesArray should be
  2. // {
  3. // id: "125325645",
  4. // name: "Captain America"
  5. // }
  6. const typesArray = [
  7. { type: 'uint8', name: 'id' },
  8. { type: 'string', name: 'name' }
  9. ];

1.使用已解码的字符串调用decodeParameters()函数

  1. const res = await web3.eth.abi.decodeParameters(typesArray, decodedString);

res将属于以下类型:

  1. {
  2. '0': '125325645',
  3. '1': 'Captain America',
  4. __length__: 2,
  5. id: "125325645",
  6. name: "Captain America"
  7. }

**其他:**如果您预期的JSON包含嵌套元素,则需要使用components,这是您声明typesArray的方式。

  1. // Example JSON:
  2. // {
  3. // people: [
  4. // {
  5. // id: '453545',
  6. // name: 'Captain America',
  7. // },
  8. // {
  9. // id: '567567',
  10. // name: 'Iron Man',
  11. // }
  12. // ]
  13. // }
  14. const typesArray = [
  15. {
  16. type: 'tuple',
  17. name: 'people',
  18. components: [
  19. { type: 'uint8', name: 'id' },
  20. { type: 'string', name: 'name' }
  21. ]
  22. }
  23. ];
展开查看全部

相关问题