在Linux上测试Swagger接口可通过以下方式操作:
Swagger UI界面测试
docker pull swaggerapi/swagger-ui:v4.15.5
docker run -d -p 38081:8080 swaggerapi/swagger-ui:v4.15.5
访问 http://localhost:38081
,在界面中找到接口,点击“TRY IT OUT”输入参数测试。http://<服务器地址>/swagger-ui.html
。命令行工具测试
curl
发送HTTP请求:# GET请求示例
curl -X GET "http://localhost:8080/api/users?id=1"
# POST请求示例
curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' http://localhost:8080/api/users
```。
脚本工具测试
swagger-hacker.py
等脚本快速探测接口:python swagger-hacker.py -u http://localhost:8080/v2/api-docs
```。
requests
库编写自动化脚本,结合pytest
断言验证响应。专业工具集成