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
下拉菜單切換。