import urllib.request
import json
import traceback

data = json.dumps({'user_query':'áo đi làm', 'conversation_id':'test-1234'}).encode('utf-8')
req = urllib.request.Request('http://localhost:5000/api/agent/chat-lead-flow', data=data, headers={'Content-Type': 'application/json'})
try:
    res = urllib.request.urlopen(req)
    out = res.read().decode('utf-8')
    with open("test_out.txt", "w", encoding="utf-8") as f:
        f.write(out)
except urllib.error.HTTPError as e:
    out = e.read().decode('utf-8')
    with open("test_out.txt", "w", encoding="utf-8") as f:
        f.write("HTTP ERROR: " + str(e) + "\n" + out)
except Exception as e:
    with open("test_out.txt", "w", encoding="utf-8") as f:
        f.write(traceback.format_exc())
