CentOS系统下Swagger接口测试方法
Swagger UI是CentOS环境下最常用的接口测试工具,提供可视化的文档展示和实时请求响应功能。
http://<服务器IP>:8080/swagger-ui.html
,若为远程访问需替换为公网IP或域名),即可打开交互式API文档页面。cURL是Linux系统自带的命令行工具,适合快速测试接口,无需图形界面。
curl "http://<服务器IP>:<端口>/<接口路径>?param1=value1¶m2=value2"
;curl -X POST "http://<服务器IP>:<端口>/<接口路径>" -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1¶m2=value2"
;curl -X POST "http://<服务器IP>:<端口>/<接口路径>" -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}'
;curl -X POST "http://<服务器IP>:<端口>/<接口路径>" -F "file=@/path/to/local/file" -F "param1=value1"
。swagger.json
);导入后,SoapUI会自动生成接口列表,点击接口旁的绿色三角按钮即可发送请求,支持断言(如状态码、响应内容)和参数化测试。swagger-hacker.py
脚本;在CentOS终端中运行python3 swagger-hacker.py -u <Swagger JSON地址>
(如python3 swagger-hacker.py -u http://<服务器IP>:<端口>/v2/api-docs
);脚本会自动发送请求并生成测试报告,包含接口的响应状态、耗时等信息。接口测试需兼顾安全性,可使用Burp Suite或X-Ray等工具扫描Swagger接口的潜在漏洞(如SQL注入、XSS攻击)。
http://127.0.0.1:8080
),将流量转发到Burp Suite或X-Ray;