Dav*_*983 4 python python-requests graphql
我在使用 GraphQL 和 Python 请求发布突变时遇到问题。
我的功能看起来像:
def create(request):
access_token = 'REDACTED'
headers = {
"X-Shopify-Storefront-Access-Token": access_token
}
mutation = """
{
checkoutCreate(input: {
lineItems: [{ variantId: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80", quantity: 1 }]
}) {
checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
"""
data = (requests.post('https://catsinuniform.myshopify.com/api/graphql', json={'mutation': mutation}, headers=headers).json())
print(data)
return render(request, 'Stock/create.html', { 'create': data })
Run Code Online (Sandbox Code Playgroud)
我收到错误消息,说我的 json 响应中有一个错误的请求“bad_request - Parameter Missing or Invalid”。
即使您发送了一个变更,您的请求正文仍应包含一个查询属性,其值应为表示您的操作的字符串。这有点令人困惑,但非正式地将查询和更改都称为“查询”(无论哪种方式,您仍然在“查询”服务器)。将您的请求更改为:
requests.post('https://catsinuniform.myshopify.com/api/graphql', json={'query': mutation}, headers=headers)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3662 次 |
| 最近记录: |