温馨提示×

linux postman怎么调试接口

小樊
50
2026-10-09 13:48:25
栏目: 智能运维

在 Linux 下用 Postman 调试接口,常见有 两种方式:
1️⃣ 使用 Postman 桌面版(GUI)
2️⃣ 使用 Postman CLI(newman) 或 curl 替代

下面按使用场景给你讲清楚。


一、Postman 桌面版(推荐,最简单)

1. 安装 Postman(Linux)

Postman 官方提供 .tar.gz 包:

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

# 解压
tar -xzf postman.tar.gz -C /opt

# 创建软链接
ln -s /opt/Postman/Postman /usr/local/bin/postman

启动:

postman

如果是 Ubuntu / Debian,也可以直接搜 Postman(Snap):

sudo snap install postman

2. 基本调试接口步骤

  1. 打开 Postman
  2. 点击 + New Request
  3. 选择请求方式:
    • GET
    • POST
    • PUT
    • DELETE
  4. 填写 URL(如 http://localhost:8080/api/user)
  5. 设置参数:
    • Params(GET 参数)
    • Headers(token、Content-Type)
    • Body(JSON / form-data)
  6. 点击 Send
  7. 查看:
    • Status Code
    • Response Body
    • Headers
    • Time / Size

3. 调试技巧(很实用)

✅ 查看请求详情

  • Console(View → Show Postman Console)
  • 能看到真实发出的请求

✅ 环境变量

  • 设置 base_url
  • 切换 dev / test / prod

✅ Pre-request Script

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

✅ Tests 断言

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

二、Linux 无界面(服务器)调试方式

方案 1:curl(最常用)

curl -X GET "http://localhost:8080/api/user" \
  -H "Authorization: Bearer token"

POST JSON:

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

方案 2:Newman(Postman CLI)

适合 自动化测试

# 安装
npm install -g newman

# 运行 Postman 导出的 collection
newman run api_test.postman_collection.json

三、常见问题

1. Linux 无法启动 Postman?

sudo apt install libgconf-2-4

或检查是否是 64 位系统


2. 本地服务连不上?

  • 确认服务监听 0.0.0.0
  • 防火墙
sudo ufw status

3. HTTPS 证书报错?

Postman → Settings → SSL certificate verification → OFF(仅测试环境)


四、总结

方式 适合场景
Postman GUI 本地开发、可视化调试
curl 服务器、快速验证
Newman 自动化接口测试

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

  • 你用的是 Ubuntu / CentOS / 服务器无界面
  • 调试的是 HTTP / HTTPS / WebSocket
  • 是 前后端联调 还是 测试接口

我可以直接给你 具体操作示例。

0 踩