API access via python script

DanScho
New Contributor III

Hello,

I try to access an API via a python script.

I have access using postman. But if I try to access it via python in databricks, it will not work (timeout).

Here is my code:

import requests
import socket

headers = {
    'Content-Type': 'application/json',
    'x-api-key': '<api-key>',
    'User-Agent': 'PostmanRuntime/7.43.0',
    'Accept': '*/*',
    'Host': '<URL to host>',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Content-Length': '105'
}
payload = {
    "result_l": "en",
    "result_n": 2,
    "texts": [
        "Text1.",
        "Text2"
    ]
}
 
    response = requests.post(url, headers=headers, json=payload, timeout=10)  # Timeout nach 10 Sekunden
    response.raise_for_status()  # Löst eine Ausnahme bei HTTP-Fehlern aus
    data = response.json()
    print(data)
 

Best regards,

Daniel