代码之家  ›  专栏  ›  技术社区  ›  AlliDeacon

GraphQL和Python with JSon-语法错误,带有未转义字符

  •  1
  • AlliDeacon  · 技术社区  · 6 年前

    # 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}]}]}
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Daniel Rearden    6 年前

    我的蟒蛇生锈了,但我相信你想用它 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)