温馨提示×

ubuntu postman如何测试接口

小樊
52
2026-10-06 22:18:52
栏目: 智能运维

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

  1. Postman 桌面版(推荐)
  2. Postman CLI(newman,适合自动化)

下面分别说明。


一、安装 Postman(Ubuntu)

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

sudo snap install postman

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

方式 2:官网下载

  1. 访问:https://www.postman.com/downloads/
  2. 选择 Linux 版本(.deb 或 tar 包)
  3. 解压并运行 Postman

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

1. 新建请求

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

2. 填写接口地址

例如:

http://localhost:8080/api/user

3. 设置请求参数

GET 请求

  • 在 Params 中添加:
    • Key: id
    • Value: 1

POST 请求

  • 切换到 Body
  • 选择:
    • raw
    • 格式选 JSON
  • 示例:
{
  "username": "test",
  "password": "123456"
}

4. 设置请求头(Headers)

常见示例:

Content-Type: application/json
Authorization: Bearer token值

5. 发送请求

点击 Send

6. 查看响应

  • Status:HTTP 状态码(200 / 400 / 500)
  • Body:返回数据
  • Headers:响应头
  • Time:请求耗时

三、测试本地接口示例

假设 Ubuntu 上运行了一个接口:

GET http://127.0.0.1:3000/api/hello

Postman 中:

  • Method: GET
  • URL: http://127.0.0.1:3000/api/hello
  • 点击 Send
  • 查看返回 JSON

四、使用环境变量(推荐)

  1. 点击 Environments → New
  2. 添加变量:
    • base_url = http://localhost:8080
  3. 请求中使用:
{{base_url}}/api/user

好处:切换测试/生产环境很方便


五、使用 Newman(命令行测试)

适合 CI / 自动化测试。

安装

sudo apt install nodejs npm
sudo npm install -g newman

导出 Postman Collection

  • Postman → 集合 → Export → 保存为 test.json

运行

newman run test.json

六、常见问题

1. 无法连接 localhost

  • 确认服务已启动
  • 检查防火墙
  • 如果是 Docker,使用容器 IP 或端口映射

2. Ubuntu 上 Postman 打不开

sudo snap refresh postman

或重新安装


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

  • 你测试的是 REST / WebSocket / GraphQL
  • 接口是 本地服务还是远程服务器
  • 使用的是 Spring / Flask / Node / Django

我可以给你一个 具体接口的 Postman 配置示例。

0 踩