GraphQL and Python with JSon - Syntax Error with Unescaped c



我正在尝试发布 GraphQL 突变,但遇到无法弄清楚的语法错误。 我看到有一个关于转义/未转义字符的错误。 我试过转义,双引号,单引号,没有引号。 我似乎无法克服这个问题。 代码下的回溯。

# 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>nn1: n   ^n","category":"graphql","locations":[{"l
ine":1,"column":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)