后GraphQL变异与Python请求

2024-09-25 00:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在发布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 })

我收到错误消息,说我的json响应中有一个错误的请求“bad\u request-Parameter Missing or Invalid”。在


Tags: 功能tokenjsondataaccessrequestdef错误
1条回答
网友
1楼 · 发布于 2024-09-25 00:26:16

即使发送的是一个变异,请求主体仍然应该包含一个查询属性,该属性的值应该是表示操作的字符串。“这两种查询都被非正式地称为“查询”。将您的请求更改为:

requests.post('https://catsinuniform.myshopify.com/api/graphql', json={'query': mutation}, headers=headers)

相关问题 更多 >