温馨提示×

ubuntu postman怎么测接口

小樊
45
2026-09-07 02:00:19
栏目: 智能运维

Ubuntu 上使用 Postman 测试接口,一般有两种方式:

  1. 图形界面 Postman(推荐新手)
  2. 命令行 Newman(适合自动化)

下面从最常用的 图形界面 Postman 讲起。


一、Ubuntu 安装 Postman

方式 1:Snap 安装(最简单)

sudo snap install postman

安装完成后,在应用菜单搜索 Postman 即可打开。

方式 2:官网下载

  1. 打开 https://www.postman.com/downloads/
  2. 下载 Linux 版本(.tar.gz
  3. 解压:
tar -xzf Postman-linux-x64-*.tar.gz
  1. 运行:
./Postman/Postman

二、用 Postman 测试接口(基础步骤)

1️⃣ 创建请求

  • 打开 Postman
  • 点击左上角 + New → Request
  • 给请求起个名字(如 登录接口

2️⃣ 设置请求方法和 URL

示例:

GET  https://api.example.com/users
POST http://localhost:8080/login

3️⃣ 设置请求参数

✅ GET 请求参数

Params 标签中:

key: page
value: 1

✅ POST 请求(JSON)

  1. 选择 POST
  2. 点击 Body → raw → JSON
  3. 示例:
{
  "username": "admin",
  "password": "123456"
}

4️⃣ 设置请求头(Headers)

常见情况:

Content-Type: application/json
Authorization: Bearer xxxxxxx

5️⃣ 发送请求

点击 Send

返回结果会在下方显示:

  • Status:200 / 400 / 500
  • Body:接口返回的数据
  • Headers:响应头

三、示例:测试本地接口

假设本地服务:

POST http://127.0.0.1:8000/api/login

Postman 配置:

  • Method: POST
  • Body (raw → JSON):
{
  "user": "test",
  "pwd": "123"
}

点击 Send,查看返回:

{
  "code": 0,
  "msg": "success"
}

四、环境变量(多环境切换)

  1. 右上角 Environment → Add
  2. 添加变量:
    • base_url = http://localhost:8080
  3. 请求中使用:
{{base_url}}/login

五、命令行测试(Newman)

如果你用 Postman 导出了 Collection:

sudo npm install -g newman
newman run my_collection.json

六、常见问题

❓ Ubuntu 打不开 Postman

sudo snap refresh postman

或检查:

./Postman/Postman --no-sandbox

❓ 请求本地接口失败

  • 确认服务在运行
  • Ubuntu 防火墙:
sudo ufw status

如果你是 测试某个具体接口(如登录 / 上传文件 / WebSocket),可以告诉我接口类型,我可以直接给你 Postman 配置示例。

0