post - 服务器不接受查询中的双引号

标签 post python-requests graphql

我正在尝试提出与 here 相同的请求使用请求和Python。这是我的代码:

address = "0x800d0c1e4fb219a2d9bd2f292aa91abdcd862915"
data= """{"query":"{poolSnapshots(where:{pool:"%s"}, orderBy: date){id baseTokenLiquidity datatokenLiquidity spotPrice}}","variables":null,"operationName":null}"""%(address)
response = requests.post('https://v4.subgraph.polygon.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph', data=data).json()

但是当我执行代码时,出现以下错误:

GraphQL server error (client error): expected ,or} at line 1 column 39: 0

我错过了什么?我发短信时双引号错误吗?谢谢各位的解答!

最佳答案

API 需要一个包含在三引号中的字符串化 JSON。正确的方法是使用json.dumps将查询有效负载 (dict) 字符串化。它将把有效负载数据类型转换为相应的 JSON 类型。

import requests
import json

address = "0x800d0c1e4fb219a2d9bd2f292aa91abdcd862915"
url = 'https://v4.subgraph.polygon.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph'

data = {
    "query": "{poolSnapshots(where:{pool:\"%s\"}, orderBy: date){id baseTokenLiquidity datatokenLiquidity spotPrice}}" % address,
    "operationName": None,
    "variables": None,
}

response = requests.post(url, data=json.dumps(data))
print(response.json())

关于post - 服务器不接受查询中的双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72861659/

相关文章:

jquery - Web API OWIN 从 $.AJAX POST withCredentials :true 接收空数据

php - 将变量传递给 .done(function()

c# - 在 C# 中使用 HttpWebRequest 发布参数

javascript - 如何使用 Ajax 调用此后操作方法?

javascript - Graphql 嵌套突变

reactjs - 是否可以将对象作为变量传递到 graphQL 突变中?

python - 使用 python requests 模块登录基于 WordPress 的网站

Python - 列表索引超出范围 -

python - 即使在添加 ETag header 后也没有收到 304 响应

java - 在 Tomcat 中注册 SimpleGraphQLServlet