在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斷言驗證響應。專業工具集成