0

I have create an AppSynch API on AWS. I can easily query it using the console or some AWS specific package. However I would want to query it using a simple package such as e.g. urllib3. Its surprisingly hard to find anyone doing using a direct api call (everyone uses some kind of aws related packages or solutions that i cant seem to get to work). The query I want to do is:

       mutation provision {
      provision(
        noteId:
        {ec2Instance: "t2.micro",
          s3Bucket: "dev"})}

I have tried with different variations of:

    query = """
    mutation provision {
      provision(
        noteId:
        {ec2Instance: "t2.micro",
          s3Bucket: "dev"})}
    """
    headers = {"x-api-key": api_key}

http = urllib3.PoolManager()

    data = json.dumps("query": query, "variables": {}, "operationName": "somename")
    
    r = http.request('POST', url, headers=headers,
                     data=data.encode('utf8'))

But I somehow cannot get it to work, i keep on getting messages that the API cant understand the POST request

smallbirds
  • 877
  • 12
  • 35

1 Answers1

0

I found the solution:

import requests
import json

    APPSYNC_API_KEY = APPSYNC_API_KEY
    APPSYNC_API_ENDPOINT_URL = APPSYNC_API_ENDPOINT_URL
    
    headers = {
        'Content-Type': "application/graphql",
        'x-api-key': APPSYNC_API_KEY,
        'cache-control': "no-cache",
    }
    
    query = """
    mutation provision {
      provision(
        inputParameters:
        {ec2Instance: "t2.micro",
          s3Bucket: "dev"})}
    """
    
    payload_obj = {"query": query}
    payload = json.dumps(payload_obj)
    response = requests.request("POST", APPSYNC_API_ENDPOINT_URL, data=payload, headers=headers)
    print(response)
smallbirds
  • 877
  • 12
  • 35