https://prefect.io logo
#prefect-community
Title
# prefect-community
k

Ken Nguyen

03/23/2022, 6:08 PM
Hi, I’m currently running into issues trying to do a simple get request for logs from the GraphQL API. Could anyone help me identify where my mistake is to get the error below? I’ve tested the query in the Prefect front end’s Interactive API and it works just fine. I’ve also tested in a Javascript get request that that API key works fine, so this may be a requests library related mistake?
k

Kevin Kho

03/23/2022, 6:10 PM
Can you paste your code here so I can test? I can give it a shot
k

Ken Nguyen

03/23/2022, 6:12 PM
Yes of course:
Copy code
import requests
import json
import pandas as pd

query = """"
query {
  flow_run (
    where: {flow_id: {_eq: "FLOW_ID"}}
    order_by: {state_timestamp: desc}
    limit: 1
  ){
    name
    flow_id
    flow {
      name
      id
    }
    logs {
      message
      created
    }
  }
}
"""

url = "<https://api.prefect.io>"
headers = {"Authorization": "Bearer " + "API_KEY"}

r = requests.get(
  url=url, 
  json=query, 
  headers=headers
)
print(r.text)
k

Kevin Kho

03/23/2022, 6:29 PM
Use a post request and make your
json
a dict that contains the query:
Copy code
r = <http://requests.post|requests.post>(
  url=url, 
  json=dict(query=query), 
  headers=headers
)
k

Ken Nguyen

03/23/2022, 6:32 PM
I’ve done both and got this error now:
{"errors":[{"message":"Syntax Error: Unterminated string.","extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}
Did you also adjust the query?
k

Kevin Kho

03/23/2022, 6:33 PM
oh yeah i was using a simpler query
k

Ken Nguyen

03/23/2022, 6:33 PM
Could you show me what your query looks like?
k

Kevin Kho

03/23/2022, 6:34 PM
You have an extra ” in the snippet you pasted at the start of the query
k

Ken Nguyen

03/23/2022, 6:34 PM
indeed I do! That worked
Thank you so much!
k

Kevin Kho

03/23/2022, 6:34 PM
Yep!
k

Ken Nguyen

03/23/2022, 6:34 PM
Very interesting that you have to use a post request, do you know the reason behind that?
k

Kevin Kho

03/23/2022, 6:34 PM
All GQL are post
k

Ken Nguyen

03/23/2022, 6:34 PM
Ahhh, today I learned!
Thanks again 🙂
👍 1
4 Views