如何在Linux下使用Postman进行接口调试
Postman支持多种方式在Linux系统上安装,以下是三种常见方法:
sudo apt update && sudo apt install snapd;再执行sudo snap install postman,等待安装完成即可。Postman-linux-x64-版本号.tar.gz);解压至/opt目录(tar -xvf Postman-*.tar.gz -C /opt);创建软链接以便终端直接调用:sudo ln -s /opt/Postman/Postman /usr/bin/postman。~/.local/share/applications/下创建postman.desktop文件,内容如下:[Desktop Entry]
Encoding=UTF-8
Name=Postman
Exec=/opt/Postman/Postman
Icon=/opt/Postman/app/resources/app/assets/icon.png
Terminal=false
Type=Application
Categories=Development;
赋予可执行权限(sudo chmod +x ~/.local/share/applications/postman.desktop),即可在应用菜单中找到Postman。打开Postman,点击左上角**+ New→HTTP Request**,进入请求编辑页面。
https://api.example.com/users)。切换至Headers选项卡,点击**+ Add Header**添加必要键值对(如Content-Type: application/json、Authorization: Bearer {token}),用于身份验证或指定数据格式。
若API需要发送数据(如POST、PUT请求),切换至Body选项卡:
raw→JSON);{"name": "John", "age": 30})。点击右侧Send按钮,下方Response区域将显示:
Content-Type: application/json);环境变量用于管理不同环境(如开发、测试、生产)的配置(如API基础URL),避免重复修改请求:
base_url: https://dev.api.example.com);{{base_url}}/users),发送请求时会自动替换为对应环境的值。pm.environment.set("timestamp", new Date().getTime());
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Response contains user name", function() {
const jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("John");
});
newman run MyCollection.json -e Development.json
其中MyCollection.json是导出的集合文件,Development.json是导出的环境文件。/opt/Postman)及软链接具有正确的读写权限;~/.config/Postman目录),避免数据丢失。