#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试目标创建接口的脚本 """ import requests import json from datetime import datetime def test_create_goal(): """测试创建目标接口""" url = "http://localhost:8080/api/goals" # 测试数据 test_data = { "userId": 1, "subject": "数学", "goalType": "练习题", "goalContent": "二次函数", "difficulty": "中等", "totalQuantity": 5, "estimatedTime": 30, "startTime": datetime.now().isoformat() } headers = { "Content-Type": "application/json" } print(f"发送请求到: {url}") print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}") print("-" * 50) try: response = requests.post(url, json=test_data, headers=headers, timeout=30) print(f"响应状态码: {response.status_code}") print(f"响应头: {dict(response.headers)}") print(f"响应内容: {response.text}") if response.status_code == 200: result = response.json() print("\n✅ 请求成功!") print(f"返回数据: {json.dumps(result, indent=2, ensure_ascii=False)}") else: print(f"\n❌ 请求失败! 状态码: {response.status_code}") except requests.exceptions.RequestException as e: print(f"\n❌ 请求异常: {e}") except Exception as e: print(f"\n❌ 其他异常: {e}") if __name__ == "__main__": test_create_goal()