gpt4 book ai didi

python - GraphQL 和 Python with Json - 带有未转义字符的语法错误

转载 作者:行者123 更新时间:2023-12-01 01:35:30 26 4
gpt4 key购买 nike

我正在尝试发布 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>\n\n1: \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)

关于python - GraphQL 和 Python with Json - 带有未转义字符的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com