使用 curl 测试 API 接口
curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{
"username": "admin",
"password": "admin"
}'
响应:
{
"session_id": "demo-session-id",
"is_admin": true
}
curl -X GET http://localhost:8080/api/dashboard \
-H "X-Session-ID: demo-session-id"
curl -X GET http://localhost:8080/api/dhcp/leases \
-H "X-Session-ID: demo-session-id"
curl -X GET http://localhost:8080/api/dhcp/bindings \
-H "X-Session-ID: demo-session-id"
curl -X POST http://localhost:8080/api/dhcp/bindings \
-H "X-Session-ID: demo-session-id" \
-H "Content-Type: application/json" \
-d '{
"mac": "00:11:22:33:44:55",
"ip": "192.168.1.100",
"hostname": "my-server",
"description": "我的服务器"
}'
curl -X DELETE http://localhost:8080/api/dhcp/bindings/1 \
-H "X-Session-ID: demo-session-id"
curl -X GET http://localhost:8080/api/dns/records \
-H "X-Session-ID: demo-session-id"
curl -X POST http://localhost:8080/api/dns/records \
-H "X-Session-ID: demo-session-id" \
-H "Content-Type: application/json" \
-d '{
"name": "test.example.com",
"type": "A",
"value": "192.168.1.100",
"ttl": 300
}'
curl -X POST http://localhost:8080/api/dns/records \
-H "X-Session-ID: demo-session-id" \
-H "Content-Type: application/json" \
-d '{
"name": "www.example.com",
"type": "CNAME",
"value": "example.com",
"ttl": 3600
}'
curl -X DELETE http://localhost:8080/api/dns/records/1 \
-H "X-Session-ID: demo-session-id"
curl -X GET http://localhost:8080/api/dns/logs \
-H "X-Session-ID: demo-session-id"
curl -X GET http://localhost:8080/api/config \
-H "X-Session-ID: demo-session-id"
curl -X PUT http://localhost:8080/api/config \
-H "X-Session-ID: demo-session-id" \
-H "Content-Type: application/json" \
-d '{
"dhcp": {
"enabled": true,
"ip_pool_start": "192.168.1.50",
"ip_pool_end": "192.168.1.250"
}
}'
import requests
BASE_URL = "http://localhost:8080"
# 登录
login_resp = requests.post(f"{BASE_URL}/api/login", json={
"username": "admin",
"password": "admin"
})
session_id = login_resp.json()["session_id"]
headers = {"X-Session-ID": session_id}
# 获取仪表盘
dashboard = requests.get(f"{BASE_URL}/api/dashboard", headers=headers)
print("Dashboard:", dashboard.json())
# 创建 DNS 记录
create_resp = requests.post(f"{BASE_URL}/api/dns/records",
headers=headers,
json={
"name": "test.local",
"type": "A",
"value": "192.168.1.100",
"ttl": 300
}
)
print("Create record:", create_resp.json())
# 获取 DNS 记录
records = requests.get(f"{BASE_URL}/api/dns/records", headers=headers)
print("DNS Records:", records.json())
导入以下集合作为快速开始:
{
"info": {
"name": "DHCP DNS Manager API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Login",
"request": {
"method": "POST",
"header": [{"key": "Content-Type", "value": "application/json"}],
"url": "http://localhost:8080/api/login",
"body": {
"mode": "raw",
"raw": "{\"username\":\"admin\",\"password\":\"admin\"}"
}
}
},
{
"name": "Get Dashboard",
"request": {
"method": "GET",
"header": [{"key": "X-Session-ID", "value": "{{session_id}}"}],
"url": "http://localhost:8080/api/dashboard"
}
}
]
}
| 状态码 | 说明 |
|---|---|
| 200 | 成功 |
| 400 | 请求参数错误 |
| 401 | 未授权(Session 无效) |
| 404 | 资源不存在 |
| 500 | 服务器内部错误 |
X-Session-ID 请求头