【问题标题】:requests.request.text returns encoded symbolsrequests.request.text 返回编码符号
【发布时间】:2022-07-09 00:42:46
【问题描述】:
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)

打印语句打印以下内容: ��D��R����Ӥ����?l���I ��h��'���x=Ϥ�d3��rϚ�^��@�S�D���Ė��s��"�TZL�yeyD�gfT"*���H��'(GD��k,�XQ��fK4f+�

我尝试了这个:

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 请求。

我应该怎么做才能解码响应?

【问题讨论】:

  • 你能分享一下你是如何知道解码后的响应应该是你在上一个提到的json
  • HTTP 请求是从在 FireFox 上执行的操作中获取的。我将 HTTP 请求导入 Postman,它会返回我在上面提交的 HTTP 响应。但是,当我尝试在 python 中实现请求时,我收到一条编码消息,即使编码是 utf-8。
  • 通过这篇文章解决了我的问题:stackoverflow.com/questions/61031952/…

标签: python json postman httpresponse


【解决方案1】:

您需要对输出进行解码。一种方法是使用来自 web3.js 的decodeParameters()

  1. 初始化 web3(遵循来自 here 的官方以太坊文档,
const Web3 = require("web3");
const web3 = new Web3("https://cloudflare-eth.com");
  1. 接下来,我们需要指定解码后的 JSON 的样子。 我们必须提到数据类型(Solidity)和参数名称。
// Example, if JSON is like this, then typesArray should be
// {
//   id: "125325645",
//   name: "Captain America"
// }

const typesArray = [
  { type: 'uint8', name: 'id' },
  { type: 'string', name: 'name' }
];
  1. 使用解码后的字符串调用 decodeParameters() 函数
const res = await web3.eth.abi.decodeParameters(typesArray, decodedString);

res 将属于以下类型:

{
  0: '125325645',
  1: 'Captain America',
  id: "125325645",
  name: "Captain America"
}

额外内容: 如果您预期的 JSON 具有嵌套元素,那么您将需要使用 components,这就是您声明 typesArray 的方式,

// Example JSON:
// {
//   people: [
//     {
//       id: '453545',
//       name: 'Captain America',
//     },
//     {
//       id: '567567',
//       name: 'Iron Man',
//     }
//   ]
// }

const typesArray = [
  {
    type: 'tuple',
    name: 'people',
    components: [
      { type: 'uint8', name: 'id' },
      { type: 'string', name: 'name' }
    ]
  }
];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多