CentOS下Postman网络请求调试全流程指南
下载与安装
访问Postman官方网站下载Linux版二进制包(如Postman-linux-x64-*.tar.gz),解压至/opt目录:
tar -xzf Postman-linux-x64-*.tar.gz -C /opt
创建全局符号链接以方便命令行调用:
sudo ln -s /opt/Postman/Postman /usr/bin/postman
启动Postman:终端输入postman即可打开应用。
解决常见安装问题
libXss.so.1缺失,安装对应库:sudo yum install libXScrnSaver
sudo运行或修改~/.config/Postman权限。构造请求
点击“New”→“HTTP Request”,填写以下关键信息:
https://api.example.com/data);Content-Type: application/json、Authorization: Bearer <token>);raw→JSON格式,输入{"key":"value"})。使用Pre-request Script与Tests调试
console.log()输出调试信息;// 验证状态码为200
pm.test("Status code is 200", () => pm.response.to.have.status(200));
// 验证响应头包含Content-Type: application/json
pm.test("Content-Type is JSON", () => {
const contentType = pm.response.headers.get("Content-Type");
pm.expect(contentType).to.include("application/json");
});
// 解析JSON并验证字段
const responseJson = pm.response.json();
pm.test("Has valid user data", () => {
pm.expect(responseJson).to.have.property("userId").that.is.a("number");
pm.expect(responseJson.token).to.be.a("string").and.not.empty;
});
console.log()输出的调试信息。sudo npm install -g newman
collection.json)和环境文件(environment.json),执行:newman run collection.json -e environment.json
--reporters html参数,自动生成可视化报告:newman run collection.json -e environment.json --reporters html --reporter-html-export report.html
run_tests.sh自动化运行:#!/bin/bash
COLLECTION="/path/to/collection.json"
ENV="/path/to/environment.json"
newman run "$COLLECTION" -e "$ENV" --reporters html --reporter-html-export "report_$(date +%Y%m%d).html"
赋予执行权限后运行:chmod +x run_tests.sh && ./run_tests.sh。网络连接失败
ping www.baidu.com);sudo firewall-cmd --permanent --zone=public --add-port=9999/tcp
sudo firewall-cmd --reload
SSL证书问题
若接口使用HTTPS且报“SSL certificate problem”,可临时关闭验证(不推荐生产环境):
请求超时
代理抓包
若需捕获Postman与服务器之间的原始请求,可使用代理工具(如Fiddler/Charles):
127.0.0.1,端口对应代理工具端口;环境变量管理
使用环境变量存储动态值(如API基础URL、Token),避免重复输入:
Dev);{{base_url}}/data),通过Environment下拉菜单切换。