温馨提示×

linux postman怎样测试接口

小樊
43
2026-10-21 04:28:29
栏目: 智能运维

在 Linux 下使用 Postman 测试接口,一般有两种方式:


一、使用 Postman 桌面版(图形界面)

1. 安装 Postman(Linux)

方式一:官网下载(推荐)

# 下载
wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gz

# 解压
tar -xzf postman.tar.gz

# 运行
./Postman/Postman

方式二:Snap 安装(Ubuntu)

sudo snap install postman

2. 使用 Postman 测试接口

  1. 打开 Postman
  2. 点击 New → Request
  3. 设置:
    • 请求方法:GET / POST / PUT / DELETE
    • URL:http://localhost:8080/api/test
  4. 如果有参数:
    • Params:URL 参数
    • Headers:请求头(如 Authorization)
    • Body:
      • form-data
      • x-www-form-urlencoded
      • raw(JSON)
  5. 点击 Send
  6. 查看:
    • 状态码(200 / 400 / 500)
    • 响应体(Response)
    • 响应时间

二、使用 Postman CLI(无界面,Linux 服务器常用)

1. 安装 Newman(Postman 命令行工具)

sudo apt install nodejs npm -y
sudo npm install -g newman

2. 导出 Postman 接口集合

  • Postman → 集合 → Export → 选择 v2.1
  • 得到 test.postman_collection.json

3. 命令行运行接口测试

newman run test.postman_collection.json

4. 带环境变量

newman run test.postman_collection.json \
  -e env.postman_environment.json

三、Linux 下常见接口测试替代方案(轻量)

如果你只是临时测接口,也可以直接用:

curl 示例

curl -X POST http://localhost:8080/api/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"123456"}'

四、常见测试场景

场景 Postman 配置
登录鉴权 Headers 加 Authorization: Bearer token
文件上传 Body → form-data → File
Cookie Cookies 面板
自动化测试 Tests 标签写 JS 断言

如果你愿意,可以告诉我:

  • 你用的是 Ubuntu / CentOS / 服务器无界面
  • 测试的是 HTTP / HTTPS / WebSocket
  • 是否要做 自动化接口测试

我可以给你更具体的示例。

0 踩