Files
musica-ia/test-python.py

60 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""
Test API connection like frontend would do
"""
import requests
import json
def test_chat():
print("Testing chat API like frontend would...")
print("Sending POST to http://localhost:8000/api/chat")
url = "http://localhost:8000/api/chat"
headers = {
"Content-Type": "application/json",
}
payload = {
"user_id": "default-user",
"message": "hola",
}
print(f"Request body: {json.dumps(payload)}")
print()
try:
response = requests.post(url, headers=headers, json=payload, timeout=30)
print(f"Response status: {response.status_code} {response.reason}")
print(f"Response OK: {response.ok}")
print()
if not response.ok:
print(f"Error: Response not OK")
print(f"Response text: {response.text}")
return False
data = response.json()
print("Response received successfully!")
print(f"Response: {json.dumps(data, indent=2, ensure_ascii=False)}")
print()
print(f"AI Response: {data.get('response', '')[:150]}...")
print()
print("✅ Success! The API is working correctly.")
return True
except requests.exceptions.RequestException as e:
print(f"❌ ERROR: {e}")
return False
except json.JSONDecodeError as e:
print(f"❌ ERROR: Failed to parse JSON: {e}")
print(f"Response text: {response.text}")
return False
except Exception as e:
print(f"❌ ERROR: {e}")
return False
if __name__ == "__main__":
success = test_chat()
exit(0 if success else 1)