API for very begginers

Hi,

I am just starting my adventure with API. I would like to write a simple program (in Python) to compare two value domains. Unfortunately my Python API query returns an empty body, although the query code is 200. Could you please help me? I don’t know if this has anything to do with each other, but the same problem occurs on the website.

Cheers,
Iwona

import requests

headers = { 
           "accept": "application/json", 
           "Authorization": "XXXXX",
           "X-CSRFToken": "YYYYYY"
           }


params = {
            "page": "1",
            "page_size": "10"
        }


response = requests.get("https://liebherr.aristotlecloud.io/api/v4/metadata/valuedomain", params=params, headers=headers,verify=False)

print(response.status_code)
print(response.json())

Screenshot 2023-05-09 130514

1 Like

Hey Iwona,

I think the issue is likely in the authorisation which you are using. When you submit your token, you need to structure the headers variable like this:

headers = {
    'Authorization': f'Token {TOKEN}',
}

The code below can be used to send a request or I’ve made a jupyter notebook which you can use. The notebook is here. You can download the file and then run it on your own computer.

# We are using the requests library to send this request. Requests has very good documentation if anything is not clear (https://requests.readthedocs.io/en/latest/)
import requests

# Please enter you token here.
TOKEN = ''
# Example url = https://aristotle.cloud/home/
# Please use your own url and enter it here.
client_url = ""


# For this example, we are trying to fetch value domains, but you can use this same format for any metadata object in aristotle.
# e.g. 'api/v4/metadata/dataset'
value_domain_endpoint = 'api/v4/metadata/valuedomain'

headers = {
    'Authorization': f'Token {TOKEN}',
}

# Create the url
url = client_url+value_domain_endpoint

# send the request
response = requests.get(url, headers=headers)


# Just print the first result. If you would like to see more you can use the value_domains object or use the request variable
value_domains = response.json()['results']
print(value_domains[0])

Please let me know if you have any issues,
Hope this helps!
Ben

Thank you! It solved my problem :slight_smile: