How to Fetch GraphQL JSON Data from a URL in Python?
I am trying to fetch GraphQL data from the URL:
https://bet.hkjc.com/ch/racing/wp/
I’ve tried using Python’s requests library to make a POST request, but I’m not sure how to structure the request to get the desired JSON data.
Here’s what I’ve tried so far:
import requests
url = "https://bet.hkjc.com/ch/racing/wp/"
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
# Example query, might need modification
query = """
{
races {
id
name
date
}
}
"""
response = requests.post(url, json={'query': query}, headers=headers)
if response.status_code == 200:
print(response.json())
else:
print(f"Failed to fetch data, status code: {response.status_code}")
However, this returns an error or no data, and I’m not sure if my query is correct or if there’s something else wrong with my request. Could anyone guide me on how to properly structure the request and the query?
Do I need to adjust the URL or headers?
How can I confirm if the endpoint supports GraphQL and how to form valid queries for it?
Any help would be greatly appreciated!
Read more here: Source link