import urllib.request import urllib.error import json url = "http://localhost:8000/api/templates?machine_model=x11&device_type=wash&template_type=common" try: req = urllib.request.Request(url) with urllib.request.urlopen(req) as response: templates = json.loads(response.read().decode('utf-8')) print(f"获取到 {len(templates)} 个模板") if templates: tpl = templates[0] print(f"测试: {tpl['name']}") # 测试预览 preview_url = "http://localhost:8000/api/templates/preview" payload = json.dumps({ "template_body": tpl['template_body'], "parameters": {} }).encode('utf-8') req2 = urllib.request.Request(preview_url, data=payload, headers={'Content-Type': 'application/json'}) with urllib.request.urlopen(req2) as response2: result = json.loads(response2.read().decode('utf-8')) text = result.get('rendered_text', '') print(f"✅ 预览成功: {len(text)}字") print(f"结果: {text[:200]}") except Exception as e: print(f"❌ 错误: {e}")