HTTP API
GoSpeedTest HTTP API 接口文档。
基础信息
- Base URL:
http://your-server:8080 - 协议: HTTP/HTTPS
- 格式: JSON
公共接口
健康检查
检查服务是否正常运行。
http
GET /health响应示例:
json
{
"status": "ok",
"timestamp": "2024-01-15T10:30:00Z"
}服务信息
获取服务器信息。
http
GET /info响应示例:
json
{
"version": "1.1.0",
"name": "GoSpeedTest",
"uptime": 3600
}测速接口
下载测试
执行下载速度测试。
http
GET /download?testId=xxx&duration=15参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
testId | string | 是 | 测试会话 ID |
duration | int | 否 | 测试时长(秒),默认 15 |
响应: 流式二进制数据
上传测试
执行上传速度测试。
http
POST /upload?testId=xxx参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
testId | string | 是 | 测试会话 ID |
请求体: 二进制数据
响应示例:
json
{
"status": "success",
"bytes": 1073741824,
"duration": 10.5
}Ping 测试
执行延迟测试。
http
GET /ping响应示例:
json
{
"status": "success",
"ping": 15
}会话接口
获取测试结果
获取指定会话的测试结果。
http
GET /results?testId=xxx参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
testId | string | 是 | 测试会话 ID |
响应示例:
json
{
"testId": "xxx",
"download": {
"speedMbps": 500,
"bytes": 1073741824,
"duration": 15
},
"upload": {
"speedMbps": 300,
"bytes": 536870912,
"duration": 10
},
"ping": 15
}历史记录
获取测试历史记录。
http
GET /history响应示例:
json
{
"history": [
{
"timestamp": "2024-01-15T10:30:00Z",
"download": 500,
"upload": 300,
"ping": 15
}
]
}性能监控
性能指标
获取服务器性能指标。
http
GET /performance响应示例:
json
{
"cpu": 15.5,
"memory": 512,
"connections": 10,
"uptime": 3600
}WebSocket 接口
实时进度
通过 WebSocket 接收实时测试进度。
javascript
const ws = new WebSocket('ws://your-server:8080/ws?testId=xxx');
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log(data);
};消息格式:
json
{
"type": "progress",
"phase": "download",
"speedMbps": 500,
"bytes": 1073741824
}错误响应
错误格式
json
{
"error": "错误描述",
"code": 400
}HTTP 状态码
| 状态码 | 说明 |
|---|---|
| 200 | 成功 |
| 400 | 请求参数错误 |
| 404 | 资源不存在 |
| 500 | 服务器内部错误 |
使用示例
JavaScript
javascript
// 健康检查
fetch('http://localhost:8080/health')
.then(response => response.json())
.then(data => console.log(data));
// Ping 测试
fetch('http://localhost:8080/ping')
.then(response => response.json())
.then(data => console.log('Ping:', data.ping));Python
python
import requests
# 健康检查
response = requests.get('http://localhost:8080/health')
print(response.json())
# Ping 测试
response = requests.get('http://localhost:8080/ping')
data = response.json()
print('Ping:', data['ping'])cURL
bash
# 健康检查
curl http://localhost:8080/health
# Ping 测试
curl http://localhost:8080/ping