温馨提示×

postman ubuntu怎么调试接口

小樊
80
2026-09-12 21:24:57
栏目: 智能运维

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

  1. Postman 桌面版(推荐)
  2. Postman CLI(Newman,用于命令行调试)

下面一步一步说明。


一、安装 Postman(Ubuntu)

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

sudo snap install postman

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

方式 2:官网下载(tar 包)

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

二、Postman 基本调试接口流程

1️⃣ 创建请求

  • 打开 Postman
  • 点击 New → HTTP Request
  • 选择请求方法:
    • GET
    • POST
    • PUT
    • DELETE

2️⃣ 填写接口地址

例如:

http://localhost:8080/api/user

3️⃣ 设置请求参数

  • Params:URL 参数(GET)
  • Headers:请求头(如 Content-Type: application/json)
  • Body:
    • raw → JSON
    {
      "username": "test",
      "password": "123456"
    }
    

4️⃣ 发送请求

点击 Send

5️⃣ 查看返回结果

  • Status:HTTP 状态码
  • Body:返回数据
  • Headers:响应头
  • Time / Size:耗时和大小

三、常见调试技巧(Ubuntu 下很实用)

✅ 查看本地接口

如果是本地服务:

curl http://localhost:8080/api/test

再用 Postman 对照调试

✅ 环境变量(Environment)

  • 新建 Environment
  • 设置变量:
    base_url = http://localhost:8080
    
  • 请求中使用:
    {{base_url}}/api/user
    

✅ Pre-request Script(请求前脚本)

pm.environment.set("token", "abc123");

✅ Tests(断言)

pm.test("状态码是200", function () {
    pm.response.to.have.status(200);
});

四、命令行调试(Newman)

适合 CI / 服务器环境

安装 Newman

sudo snap install newman
# 或
npm install -g newman

导出 Postman Collection

Postman → 集合 → Export

运行

newman run example.postman_collection.json

五、Ubuntu 常见问题

❌ 打不开 Postman

sudo apt install libnss3

❌ 无法访问本地服务

  • 检查服务是否启动
  • 检查防火墙
  • 使用 127.0.0.1 而不是 localhost

如果你有 具体接口类型(Java / Python / Node) 或 报错信息,可以贴出来,我可以帮你更精准地调试。

0 踩