Can't access API programmatically?

I have a new and relatively empty Omeka S installation where I can access API results in a browser window but with a python script I get “‘Connection aborted.’, RemoteDisconnected(‘Remote end closed connection without response’”

My API url: https://maevekane.net/mni-archive/api/items

My code:

import requests
import json

url = "https://maevekane.net/mni-archive/api/items"
try:
    results = requests.get(url) # make a variable that will call the url and contain the data that is requested
    results.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
    data = results.json()
    print(data)
except requests.exceptions.ConnectionError as e:
    print(f"Connection error. Details: {e}")
except requests.exceptions.Timeout as e:
    print(f"Timeout Error. Details: {e}")
except requests.exceptions.RequestException as e:
    print(f"An unexpected error occurred during the request. Details: {e}")
except json.JSONDecodeError:
    print("Error: Could not decode JSON from the response. The server might have returned non-JSON data or an empty response.")

I can get other Omeka API results (eg, https://wardepartmentpapers.org/api/items) just fine with this code so I think it must be something with my own Omeka installation? The items are all public so I don’t think it’s an authentication issue.

Running Omeka S version 4.2.0, Reclaim hosting

Hi, maybe you could try to add headers to your request so it does not get rejected? There are plenty of examples over the web.

1 Like

Try this:

import requests
import json

url = "https://maevekane.net/mni-archive/api/items"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}

try:
    results = requests.get(url, headers=headers, timeout=30)
    results.raise_for_status()
    data = results.json()
    print(data)
except requests.exceptions.ConnectionError as e:
    print(f"Connection error. Details: {e}")
except requests.exceptions.Timeout as e:
    print(f"Timeout Error. Details: {e}")
except requests.exceptions.RequestException as e:
    print(f"An unexpected error occurred during the request. Details: {e}")
except json.JSONDecodeError:
    print("Error: Could not decode JSON from the response.")

If that still doesn’t help, the issue may be related to TLS/SSL and how your Python environment handles the server certificate. You can test this hypothesis by temporarily adding verify=False to your requests.get() call (not for production, but for diagnostic purposes only).If setting verify=False resolves the issue, the actual cause is likely the SSL certificate chain, and the solution would be to update the certifi package (pip install --upgrade certifi) or install the certificate package for your operating system.

1 Like

It was the lack of headers, thank you both. I was able to access other omeka sites without headers so didn’t know I’d need them for my own install.