【问题标题】:GraphQL and Python with JSon - Syntax Error with Unescaped characters带有 JSon 的 GraphQL 和 Python - 未转义字符的语法错误
【发布时间】:2019-02-25 06:13:27
【问题描述】:

我正在尝试发布 GraphQL Mutation,但遇到了我无法弄清楚的语法错误。我看到有一个关于转义/未转义字符的错误。我试过转义,双引号,单引号,不加引号。我似乎无法解决这个问题。 Traceback 下的代码。

# http headers for api call
headers = {
    'accept': "application/json",
    'content-type':"application/json",
    'authorization': "bearer " + token,
}

# create inventory variable for mutation 
# will convert the csv to the json input in production
inventory:[{"supplier_id":24522,"supplier_part_number":"1-1002-9-SN","quantity_on_hand":5,"item_next_availability_date":"05-01-2018T00:00:00", "discontinued":true}]


# payload of the json query to pass to the API
#GraphQL Query to pull in Purchase Order Data 
payload = '''
{"query":"mutation save_inventory($inventory: [inventoryInput]!) {
  inventory {
    save(inventory: $inventory, dry_run: true) {
      handle
    }
  }
}"}
'''

# send API call and assign response to variable
response = requests.post(api_url, data=payload, headers=headers)

错误我无法在下面弄清楚。

  {"errors":[{"message":"Syntax Error GraphQL (1:1) Unexpected <EOF>\n\n1: \n   ^\n","category":"graphql","locations":[{"l
    ine":1,"column":1}]}]}

【问题讨论】:

  • 会不会是payload = '''后面的换行符?如果将其更改为 payload = '''{"query":"mutat... 会发生什么?
  • 这让我克服了那个错误并进入下一个错误,谢谢! @jDo

标签: python json graphql


【解决方案1】:

我的 Python 生锈了,但我相信你想使用 json 而不是 data。您也没有传入您定义的 inventory 变量。尝试类似:

json = {
  "query": '''   
    mutation save_inventory($inventory: [inventoryInput]!) {
      inventory {
        save(inventory: $inventory, dry_run: true) {
        handle
      }
    }
  }
  ''',
  "variables": {
    "inventory": inventory
  }
}

response = requests.post(api_url, json=json, headers=headers)

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2021-07-23
    • 1970-01-01
    • 2016-04-25
    • 2020-09-12
    • 2020-01-22
    • 1970-01-01
    • 2020-11-29
    • 2017-03-16
    相关资源
    最近更新 更多