Home > Software design >  Server doesn't accept double quotes in the query
Server doesn't accept double quotes in the query

Time:07-06

I'm trying to make the same request as here using requests and python. This is my code:

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()

But when I execute the code I got the following error:

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

What am I missing? I'm texting the double quotes wrongly? Thanks for answers!

CodePudding user response:

The API expects a stringified JSON that you wrapped into the triple quotes. The right way is to use json.dumps to stringify the query payload (dict). It will convert the payload data types to corresponding JSON types.

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())
  • Related