PostMan向FastAPI发送信息测试
使用Postman测试FastAPI相似度计算接口的详细步骤:
1. 启动FastAPI服务器
首先确保您的FastAPI服务器正在运行:
bash
python main.py
# 或者
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
2. 在Postman中创建新请求
步骤1:创建新的POST请求
- 打开Postman
- 点击 "New" → "Request"
- 选择 "POST" 方法
- 输入URL:
http://localhost:8000/similarity
步骤2:设置请求头
在 "Headers" 标签页中添加:
- Key:
Content-Type - Value:
application/json
步骤3:设置请求体
在 "Body" 标签页中:
- 选择 "raw"
- 选择格式为 "JSON"
- 输入测试数据:
json
{
"question": "北京是中国的首都吗?",
"checks": [
"北京是中国的政治中心。",
"中国的首都是北京。",
"上海是中国的经济中心。",
"天津是中国的直辖市。"
]
}
3. 完整的Postman测试配置截图说明
# Postman测试FastAPI相似度计算接口完整指南
## 配置说明
### 1. 基本请求设置
- **方法**: POST
- **URL**: `http://localhost:8000/similarity`
- **端口**: 8000 (确保与您的FastAPI服务器端口一致)
### 2. Headers配置
Key: Content-Type Value: application/json
### 3. Body配置 (选择raw + JSON)
#### 测试案例1:基础测试
```json
{
"question": "北京是中国的首都吗?",
"checks": [
"北京是中国的政治中心。",
"中国的首都是北京。",
"上海是中国的经济中心。"
]
}
测试案例2:英文测试
json
{
"question": "What is the capital of China?",
"checks": [
"Beijing is the capital of China.",
"Shanghai is a major city in China.",
"China's political center is Beijing."
]
}
测试案例3:空列表测试
json
{
"question": "测试问题",
"checks": []
}
测试案例4:单个检查项测试
json
{
"question": "今天天气怎么样?",
"checks": [
"今天阳光明媚,温度适宜。"
]
}
期望的响应格式
成功响应 (Status: 200)
json
{
"similarities": [0.234, 0.876, 0.123]
}
错误响应示例
422 Validation Error (请求格式错误)
json
{
"detail": [
{
"loc": ["body", "question"],
"msg": "field required",
"type": "value_error.missing"
}
]
}
500 Internal Server Error (服务器错误)
json
{
"detail": "计算错误: 具体错误信息"
}
测试步骤检查清单
服务器检查
- [ ] FastAPI服务器已启动
- [ ] 服务器运行在正确端口 (默认8000)
- [ ] 控制台没有错误信息
- [ ] 访问
http://localhost:8000/docs能看到API文档
Postman配置检查
- [ ] 请求方法设置为 POST
- [ ] URL正确:
http://localhost:8000/similarity - [ ] Headers包含
Content-Type: application/json - [ ] Body选择了 raw 和 JSON 格式
- [ ] JSON格式正确,没有语法错误
请求数据检查
- [ ]
question字段存在且为字符串 - [ ]
checks字段存在且为字符串数组 - [ ] JSON语法正确(使用JSON验证器检查)
常见问题排查
1. 连接被拒绝 (Connection Refused)
原因: 服务器未启动或端口错误 解决:
- 检查FastAPI服务器是否运行
- 确认端口号是否正确
- 检查防火墙设置
2. 404 Not Found
原因: URL路径错误 解决:
- 确认URL为
/similarity不是/similarities - 检查服务器路由定义
3. 422 Unprocessable Entity
原因: 请求数据格式不符合预期 解决:
- 检查JSON格式是否正确
- 确认字段名称拼写正确
- 验证数据类型匹配
4. 500 Internal Server Error
原因: 服务器代码执行出错 解决:
- 查看服务器控制台错误日志
- 检查相似度计算逻辑
- 验证依赖库是否正确安装
高级测试技巧
1. 环境变量设置
在Postman中设置环境变量:
- 变量名:
base_url - 值:
http://localhost:8000 - 使用:
{{base_url}}/similarity
2. 预请求脚本
javascript
// 在Pre-request Script中添加时间戳
pm.globals.set("timestamp", new Date().toISOString());
3. 测试脚本
javascript
// 在Tests标签页中添加自动化测试
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response has similarities array", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('similarities');
pm.expect(jsonData.similarities).to.be.an('array');
});
pm.test("Similarities are numbers", function () {
const jsonData = pm.response.json();
jsonData.similarities.forEach(function(sim) {
pm.expect(sim).to.be.a('number');
pm.expect(sim).to.be.at.least(0);
pm.expect(sim).to.be.at.most(1);
});
});
4. 批量测试数据
创建Collection Runner进行批量测试:
json
[
{
"question": "测试问题1",
"checks": ["答案1", "答案2"]
},
{
"question": "测试问题2",
"checks": ["答案3", "答案4", "答案5"]
}
]
性能测试
1. 响应时间监控
在Tests中添加:
javascript
pm.test("Response time is less than 5000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(5000);
});
2. 大量数据测试
json
{
"question": "性能测试问题",
"checks": [
// 添加100个测试句子进行压力测试
"测试句子1", "测试句子2", "..."
]
}
记住在进行测试时,始终检查服务器端的日志输出,这将帮助您快速定位问题所在。
## 4. 发送请求并查看结果
点击 "Send" 按钮后,您应该能看到:
### 成功响应示例:
```json
{
"similarities": [0.234, 0.876, 0.123, 0.456]
}
可能的错误响应:
422 验证错误(请求格式不正确):
json
{
"detail": [
{
"loc": ["body", "question"],
"msg": "field required",
"type": "value_error.missing"
}
]
}
5. 调试技巧
在Postman中添加测试脚本:
在 "Tests" 标签页中添加:
javascript
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response has similarities", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('similarities');
});
pm.test("Response time is acceptable", function () {
pm.expect(pm.response.responseTime).to.be.below(5000);
});
检查请求详情:
在发送请求后,查看:
- Status: 应该是 200 OK
- Time: 响应时间
- Size: 响应大小
- Body: 返回的JSON数据
6. 常见问题排查
如果收到 "Connection refused" 错误:
- 确认FastAPI服务器正在运行
- 检查端口号是否正确(默认8000)
- 尝试访问
http://localhost:8000/docs确认服务器状态
如果收到422错误:
- 检查JSON格式是否正确
- 验证字段名称(
question和checks) - 确认数据类型正确
如果收到500错误:
- 查看FastAPI服务器控制台的错误日志
- 检查相似度计算逻辑是否有问题
7. 多个测试案例
您可以保存多个请求来测试不同场景:
测试案例1:正常情况
json
{
"question": "什么是机器学习?",
"checks": [
"机器学习是人工智能的一个分支。",
"深度学习属于机器学习的范畴。",
"今天天气很好。"
]
}
测试案例2:空检查列表
json
{
"question": "测试问题",
"checks": []
}
测试案例3:单个检查项
json
{
"question": "你好吗?",
"checks": ["我很好,谢谢!"]
}
使用这些步骤,您就可以在Postman中完整测试您的相似度计算API了。记得在测试时保持FastAPI服务器运行,并观察服务器端的日志输出来帮助调试。

浙公网安备 33010602011771号