如何在CentOS上部署Postman API
Postman作为API测试工具,需先在CentOS系统上安装客户端。以下是四种常用安装方式:
sudo rpm --import https://dl.pstmn.io/release/key.asc/etc/yum.repos.d/postman.repo,内容如下:[postman]
name=Postman Repository
baseurl=https://dl.pstmn.io/download/linux
enabled=1
gpgcheck=1
gpgkey=https://dl.pstmn.io/release/key.asc
sudo yum install postman -ypostman即可打开应用。sudo yum install snapd -y && sudo systemctl enable --now snapd.socketsudo ln -s /var/lib/snapd/snap /snapsudo snap install postman --classicpostman即可运行。Linux 64-bit版本,通过wget命令下载(以最新版为例):wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gzsudo mkdir -p /opt/postman && sudo tar -xzvf postman.tar.gz -C /opt/postman --strip-components=1/usr/share/applications/postman.desktop,内容如下:[Desktop Entry]
Encoding=UTF-8
Name=Postman
GenericName=API Tools
Comment=Postman API Client
Exec=/opt/postman/Postman
Icon=/opt/postman/app/resources/app/assets/icon.png
Terminal=false
Type=Application
Categories=Development;
StartupNotify=true
sudo chmod +x /usr/share/applications/postman.desktop,双击桌面图标或在终端输入/opt/postman/Postman启动。.rpm文件(如postman-10.0.0.rpm)。sudo rpm -ivh postman-*.rpmpostman即可运行。安装完成后,通过以下步骤实现API的测试、集合管理与自动化:
https://api.example.com/users/1)。Content-Type: application/json)、请求体(POST请求时可选择raw→JSON格式输入数据)。base_url=https://api.example.com、api_key=123456),点击Add保存。{{base_url}}/users/1),切换环境时变量值会自动替换。pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response contains user data", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("id");
pm.expect(jsonData.name).to.be.a("string");
});
通过Newman(Postman命令行工具)实现API自动化测试,适合CI/CD流程:
sudo yum install -y nodejs npmsudo npm install -g newmanCollection Format v2.1,保存为collection.json。Environment Format v2.1,保存为environment.json。newman run collection.json -e environment.jsonnewman run collection.json -e environment.json --reporters html --reporter-html-export report.htmlreport.html,包含测试结果详情。/etc/systemd/system/postman-newman.service,内容如下:[Unit]
Description=Newman API Test Service
After=network.target
[Service]
ExecStart=/usr/local/bin/newman run /path/to/collection.json -e /path/to/environment.json
Restart=always
User=your_username
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload && sudo systemctl enable postman-newman && sudo systemctl start postman-newmansudo systemctl status postman-newman通过以上步骤,可在CentOS上完成Postman客户端的部署,并实现API的测试、管理与自动化,满足开发、测试团队的需求。