【问题标题】:Post GraphQL mutation with Python Requests使用 Python 请求发布 GraphQL 突变
【发布时间】:2019-04-07 11:02:10
【问题描述】:

我在使用 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_request - Parameter Missing or Invalid”。

【问题讨论】:

  • 在发布问题和答案时,请注意省略凭据或令牌/密钥等敏感信息。
  • 您似乎发布了敏感/私人信息。请重置您的密码和/或撤销 API 密钥和令牌。您还可以编辑信息并标记您的帖子,以便版主编辑编辑历史记录。
  • 这个塞缪尔有点晚了!

标签: python python-requests graphql


【解决方案1】:

即使您正在发送突变,您的请求正文仍应包含一个查询属性,其值应该是代表您的操作的字符串。这有点令人困惑,但非正式地,查询和突变都称为“查询”(无论哪种方式,您仍在“查询”服务器)。将您的请求更改为:

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

【讨论】:

    猜你喜欢
    • 2020-09-02
    • 2021-07-08
    • 2021-10-24
    • 2019-01-08
    • 2020-03-03
    • 2020-03-05
    • 2020-02-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多