Search code examples
pythonrequestpayload

Dynamic payload in Python request


I have a request in Python and want to make it dynamic. It is about the payload query, how can I add a variable in my payload? I've tried to use .format but this doesn't work.

url = "https://graphql.bitquery.io"
payload = "{\"query\":\"{\\r\\n  ethereum(network: bsc) {\\r\\n    dexTrades(\\r\\n      options: {limit: 100, desc: \\\"tradeAmount\\\"}\\r\\n      date: {after: \\\"2021-04-30\\\"}\\r\\n      buyCurrency: {in: \\\"0xe7a39e210f067caad7992e6866beceb95b4394f7\\\"}\\r\\n    ) {\\r\\n      transaction {\\r\\n        hash\\r\\n      }\\r\\n      date {\\r\\n        date\\r\\n      }\\r\\n      buyAmount\\r\\n      buyAmountInUsd: buyAmount(in: USD)\\r\\n      buyCurrency {\\r\\n        symbol\\r\\n        address\\r\\n        tokenId\\r\\n        tokenType\\r\\n        decimals\\r\\n        name\\r\\n      }\\r\\n      sellAmount\\r\\n      sellCurrency {\\r\\n        symbol\\r\\n        address\\r\\n      }\\r\\n      sellAmountInUsd: sellAmount(in: USD)\\r\\n      tradeAmount(in: USD)\\r\\n      smartContract {\\r\\n        address {\\r\\n          address\\r\\n          annotation\\r\\n        }\\r\\n        protocolType\\r\\n      }\\r\\n    }\\r\\n  }\\r\\n}\\r\\n\",\"variables\":{}}"
headers = {
    'X-API-KEY': '',
    'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)

Solution

  • I think if I am understanding this correctly, the graphql api is taking a json formatted string and you want to modify the query part of the json. If you split up the two and provide %s formatting for the desired variables you wish to change in the payload, it should work.

    I have used date and desc as examples but any other variables you wish to change this method can also be used.

    import json
    import requests
    
    date = '2021-04-30'
    desc = 'tradeAmount'
    query = """
    
    {
      ethereum(network: bsc) {
        dexTrades(
          options: { limit: 100, desc: "%s" }
          date: { after: "%s" }
          buyCurrency: { in: "0xe7a39e210f067caad7992e6866beceb95b4394f7" }
        ) {
          transaction {
            hash
          }
          date {
            date
          }
          buyAmount
          buyAmountInUsd: buyAmount(in: USD)
          buyCurrency {
            symbol
            address
            tokenId
            tokenType
            decimals
            name
          }
          sellAmount
          sellCurrency {
            symbol
            address
          }
          sellAmountInUsd: sellAmount(in: USD)
          tradeAmount(in: USD)
          smartContract {
            address {
              address
              annotation
            }
            protocolType
          }
        }
      }
    }
    
    
    """ % (desc, date)
    
    d = {'query': query, 'variables': {}}
    payload = json.dumps(d)
    
    url = "https://graphql.bitquery.io"
    headers = {
        'X-API-KEY': '',
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)